raz / commit 1d90e30f

commit
1d90e30fc9d40fe7ddb960d62dfb01958fbbcca5
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
e514b239
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.

patch · log

10 files changed, +371 -21

PLAN.md (history) modified +1 -1

@@ -150,7 +150,7 @@
150150 - [x] 9b. Assembly procedures in place of byte procedures: the
151151 target's own syntax (appendix A), for the instructions the
152152 encoder knows and a few of the system's, bytes for the rest.
153-- [ ] 9c. `raz -S`: each line of source with its instructions, named
153+- [x] 9c. `raz -S`: each line of source with its instructions, named
154154 by the encoder as it makes them, not by a disassembler.
155155 - [ ] 9d. The trap report: on a failed check or a trap, the calls
156156 under way, each with its variables by name and value, from

README.md (history) modified +4 -2

@@ -70,7 +70,7 @@
7070
7171 ## Using it
7272
73- raz [-I dir]... [-o file] [-Werror] Main.raz
73+ raz [-I dir]... [-o file] [-Werror] [-S] Main.raz
7474 raz run [-I dir]... [-Werror] Main.raz [arg]...
7575 raz --version
7676
@@ -86,7 +86,9 @@
8686 declared, a name spelled almost alike. The compiler goes on to find
8787 the others, up to 20. It warns of what is declared and never used,
8888 and of a variable assigned but never read, unless its name starts with
89-`_`; `-Werror` makes warnings errors.
89+`_`; `-Werror` makes warnings errors. `-S` prints the main module's
90+code instead of writing it: each line of source, and under it the
91+instructions made for it.
9092
9193 At run time, a failed check says where it is and what failed, as in
9294 `Main.raz:12:9: index 7 is out of range for a length of 5`, and ends

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

@@ -8,17 +8,17 @@
88
99 | File | Lines | Code | What |
1010 |---|---|---|---|
11-| `src/diag.pas` | 274 | 198 | Messages: positions, source excerpts, colour, counts |
11+| `src/diag.pas` | 296 | 218 | Messages: positions, source excerpts, colour, counts |
1212 | `src/scan.pas` | 851 | 722 | Symbols, literals, comments, positions |
1313 | `src/sym.pas` | 596 | 497 | Types, objects, scopes, layout |
1414 | `src/chunk.pas` | 244 | 192 | The compiled program: chunks, fixups, line notes |
15-| `src/x64.pas` | 834 | 694 | The instruction encoder, jumps within a procedure, the assembler |
16-| `src/gen.pas` | 1,319 | 1,037 | The stack machine, for x86-64: values, operations, checks, calls, frames |
15+| `src/x64.pas` | 1,039 | 877 | The instruction encoder, jumps within a procedure, the assembler |
16+| `src/gen.pas` | 1,321 | 1,039 | The stack machine, for x86-64: values, operations, checks, calls, frames |
1717 | `src/item.pas` | 1,318 | 1,100 | Items: types, constants, variables, operators, conversions, calls |
1818 | `src/parse.pas` | 2,529 | 2,267 | Declarations, statements, expressions, built-ins, recovery, warnings |
1919 | `src/link.pas` | 180 | 139 | Tree shaking, layout, patching, the places |
2020 | `src/elf.pas` | 424 | 351 | Static and dynamic ELF, symbol table, line numbers |
21-| `src/raz.pas` | 188 | 141 | The command line |
21+| `src/raz.pas` | 195 | 147 | The command line |
2222
2323 Every file opens with a comment that says what it is, what it depends
2424 on and what it leaves out. Read those first; they are the map.
@@ -243,6 +243,13 @@
243243 `db` gives bytes it doesn't know. `RegNames` and `CondNames` are the
244244 names of the registers and conditions.
245245
246+The listing, `raz -S`: with `listing` set, each routine of the encoder
247+says, with `Say`, the instruction it is about to write, as the
248+assembler would read it; `List` prints the main module's live
249+procedures with each line of source (`LineText` in `diag.pas`) and the
250+instructions under it. Nothing is disassembled: the listing is what
251+the encoder meant.
252+
246253 Jumps within a procedure don't wait for the linker. A label not yet
247254 placed is a chain of the jumps to it, each jump's 32-bit field holding
248255 the position of the one before (`Jmp`, `Jcc`); `FixLink` walks the
@@ -540,7 +547,8 @@
540547 start-up does, which sets C up and calls back a `main` that runs the
541548 module bodies and the program's `main`.
542549
543-`raz run` writes the executable to a memfd, a file only in memory, and
550+`raz -S` lists the code (`List`, in `x64.pas`) instead of writing
551+it. `raz run` writes the executable to a memfd, a file only in memory, and
544552 executes it through `/proc/self/fd` in raz's own place, handing it the
545553 rest of raz's `argv`. Nothing is left on disk, and the program's
546554 status is the command's.

src/diag.pas modified +22 -0

@@ -42,6 +42,7 @@
4242 function LineOf(const p: TPos): longint;
4343 function Join(const p, q: TPos): TPos;
4444 function Source(const p: TPos): shortstring;
45+function LineText(const path: shortstring; line: longint): shortstring;
4546 procedure Error(const p: TPos; const msg: shortstring);
4647 procedure Warning(const p: TPos; const msg: shortstring);
4748 procedure Note(const p: TPos; const msg: shortstring);
@@ -210,6 +211,27 @@
210211 end;
211212
212213 { Messages }
214+
215+{ Line line of the file at path, as written }
216+function LineText(const path: shortstring; line: longint): shortstring;
217+var i, k, n: longint; s: shortstring;
218+begin
219+ s := '';
220+ for i := 0 to numFiles - 1 do
221+ if files[i].path = ShownPath(path) then begin
222+ k := 0; n := 1;
223+ while (n < line) and (k < files[i].len) do begin
224+ if files[i].buf[k] = #10 then inc(n);
225+ inc(k);
226+ end;
227+ while (k < files[i].len) and (files[i].buf[k] <> #10) and (length(s) < 255) do begin
228+ s := s + files[i].buf[k];
229+ inc(k);
230+ end;
231+ break;
232+ end;
233+ LineText := s;
234+end;
213235
214236 procedure Error(const p: TPos; const msg: shortstring);
215237 begin

src/gen.pas modified +2 -0

@@ -1139,6 +1139,7 @@
11391139 NoteLine(line, 1); { a declaration starts a line }
11401140 Push(RBP);
11411141 MovRR(RBP, RSP);
1142+ Say('sub rsp, frame');
11421143 Put1($48); Put1($81); Put1($EC); { sub rsp, imm32 }
11431144 frameFix := pc; Put4(0);
11441145 n := (AlignUp(frameMax, 8) - paramTop) div 8;
@@ -1217,6 +1218,7 @@
12171218 if n < 16 then n := 16;
12181219 if n > high(longint) then Mark('frame too big');
12191220 Put4At(frameFix, n);
1221+ Restate(frameFix - 3, 'sub rsp, ' + IntStr(n));
12201222 EndChunk(c);
12211223 end;
12221224

src/raz.pas modified +11 -4

@@ -1,12 +1,13 @@
11 { raz.pas: the command line.
22
3- raz [-I dir]... [-o file] Main.raz
3+ raz [-I dir]... [-o file] [-Werror] [-S] Main.raz
44 raz run [-I dir]... Main.raz [arg]...
55 raz --version
66
77 Compiles the main module and every module it imports, writes the
88 entry point, links what it reaches, and writes the executable: by
9- default the main module's file without its .raz. raz run writes it
9+ default the main module's file without its .raz; with -S, it lists
10+ the main module's code instead. raz run writes it
1011 to a memfd instead, a file that lives only in memory, and executes
1112 it in raz's place with the arguments after Main.raz, so its status
1213 is the program's; its argv[0] is Main.raz.
@@ -25,7 +26,7 @@
2526
2627 {$mode objfpc}{$H-}
2728
28-uses math, BaseUnix, syscall, scan, diag, sym, chunk, gen, parse, link, elf;
29+uses math, BaseUnix, syscall, scan, diag, sym, chunk, x64, gen, parse, link, elf;
2930
3031 const
3132 { what git describe says, made by make: 0.3.0 at the tag v0.3.0,
@@ -115,6 +116,8 @@
115116 outPath := ParamStr(i);
116117 end else if s = '-Werror' then
117118 werror := true
119+ else if s = '-S' then
120+ listing := true
118121 else if s = '--version' then begin
119122 writeln('raz ', Version, ', report ', Report);
120123 Halt(0);
@@ -128,7 +131,7 @@
128131 inc(i);
129132 end;
130133 if path = '' then
131- Fatal('usage: raz [-I dir]... [-o file] [-Werror] Main.raz, or raz run [options] Main.raz [arg]...');
134+ Fatal('usage: raz [-I dir]... [-o file] [-Werror] [-S] Main.raz, or raz run [options] Main.raz [arg]...');
132135 if run and (outPath <> '') then Fatal('raz run takes no -o');
133136 dir := ParamStr(0);
134137 i := length(dir);
@@ -172,6 +175,10 @@
172175 if chunks^[i].live and chunks^[i].reals then
173176 Error(chunks^[i].realsAt, 'this target has no reals');
174177 if errors > 0 then Finish;
178+ if listing then begin
179+ List(m^.sname);
180+ exit;
181+ end;
175182
176183 mainPath := path;
177184 if run then begin

src/x64.pas modified +214 -9

@@ -45,6 +45,7 @@
4545
4646 var
4747 labelAt: longint; { the last place a jump was made to land }
48+ listing: boolean; { -S: each instruction's text is kept, for List }
4849
4950 function Here: TMem;
5051 function Mem(base: longint; disp: int64): TMem;
@@ -120,6 +121,9 @@
120121 'be', 'a', 's', 'ns', 'p', 'np', 'l', 'ge', 'le', 'g');
121122
122123 procedure Assemble(constExpr: TConstFn);
124+procedure Say(const s: shortstring);
125+procedure Restate(at: longint; const s: shortstring);
126+procedure List(module: longint);
123127
124128 implementation
125129
@@ -216,15 +220,107 @@
216220 Prefix(pfx, w, reg, NoIndex, rm, byteReg and ((reg in [4..7]) or (rm in [4..7])));
217221 Opcode(op);
218222 Put1($C0 or (reg and 7) shl 3 or (rm and 7));
223+end;
224+
225+{ The listing: the text of each instruction, as it is made }
226+
227+type
228+ TSaid = record
229+ at: longint; { where the instruction starts in code }
230+ text: string[63];
231+ end;
232+
233+var
234+ said: ^TSaid;
235+ numSaid, saidCap: longint;
236+
237+procedure SayAt(at: longint; const s: shortstring);
238+begin
239+ if not listing then exit;
240+ if numSaid = saidCap then begin
241+ saidCap := saidCap * 2 + 1024;
242+ said := ReAllocMem(said, saidCap * SizeOf(TSaid));
243+ end;
244+ said[numSaid].at := at;
245+ said[numSaid].text := s;
246+ inc(numSaid);
219247 end;
220248
249+{ The text of the instruction about to be written }
250+procedure Say(const s: shortstring);
251+begin
252+ SayAt(pc, s);
253+end;
254+
255+{ The text of the instruction at at, said again, as when its operand is
256+ patched }
257+procedure Restate(at: longint; const s: shortstring);
258+var i: longint;
259+begin
260+ i := numSaid - 1;
261+ while (i >= 0) and (said[i].at <> at) do dec(i);
262+ if i >= 0 then said[i].text := s;
263+end;
264+
265+function RN(r: longint; size: longint = 8): shortstring;
266+begin
267+ case size of
268+ 1: RN := RegNames[3, r];
269+ 2: RN := RegNames[2, r];
270+ 4: RN := RegNames[1, r];
271+ else RN := RegNames[0, r];
272+ end;
273+end;
274+
275+function XN(x: longint): shortstring;
276+begin
277+ XN := 'xmm' + IntStr(x);
278+end;
279+
280+function ChunkName(c: longint): shortstring;
281+begin
282+ with chunks^[c] do
283+ if name < 0 then ChunkName := 'data' + IntStr(c)
284+ else if module < 0 then ChunkName := NameOf(name)
285+ else ChunkName := NameOf(module) + '.' + NameOf(name);
286+end;
287+
288+{ A memory operand, with the size of what it holds, as the assembler
289+ reads it }
290+function MN(const m: TMem; size: longint = 0): shortstring;
291+var s: shortstring;
292+begin
293+ case size of
294+ 1: s := 'byte ';
295+ 2: s := 'word ';
296+ 4: s := 'dword ';
297+ 8: s := 'qword ';
298+ else s := '';
299+ end;
300+ if m.base = RIP then s := s + '[' + ChunkName(m.chunk)
301+ else s := s + '[' + RN(m.base);
302+ if m.index <> NoIndex then s := s + ' + ' + RN(m.index) + ' * ' + IntStr(m.scale);
303+ if m.disp > 0 then s := s + ' + ' + IntStr(m.disp)
304+ else if m.disp < 0 then s := s + ' - ' + IntStr(-m.disp);
305+ MN := s + ']';
306+end;
307+
308+const
309+ AluNames: array[0..7] of string[3] = ('add', 'or', 'adc', 'sbb', 'and', 'sub', 'xor', 'cmp');
310+ ShiftNames: array[0..7] of string[3] = ('rol', 'ror', 'rcl', 'rcr', 'shl', 'shr', 'sal', 'sar');
311+ UnaryNames: array[2..7] of string[4] = ('not', 'neg', 'mul', 'imul', 'div', 'idiv');
312+
221313 procedure MovRR(dst, src: longint);
222314 begin
315+ if listing then Say('mov ' + RN(dst) + ', ' + RN(src));
223316 OpR(0, true, $89, src, dst);
224317 end;
225318
226319 procedure MovRI(r: longint; v: int64);
227320 begin
321+ if not listing then
322+ else if v = 0 then Say('xor ' + RN(r, 4) + ', ' + RN(r, 4))
323+ else Say('mov ' + RN(r) + ', ' + IntStr(v));
228324 if (v >= low(longint)) and (v <= high(longint)) then begin
229325 if v = 0 then OpR(0, false, $31, r, r) { xor r32, r32 }
230326 else if v > 0 then begin { mov r32, imm32 }
@@ -241,6 +337,12 @@
241337
242338 procedure Load(r: longint; const m: TMem; size: longint; signed: boolean);
243339 begin
340+ if not listing then
341+ else if size = 8 then Say('mov ' + RN(r) + ', ' + MN(m))
342+ else if (size = 4) and not signed then Say('mov ' + RN(r, 4) + ', ' + MN(m))
343+ else if size = 4 then Say('movsxd ' + RN(r) + ', ' + MN(m, 4))
344+ else if signed then Say('movsx ' + RN(r) + ', ' + MN(m, size))
345+ else Say('movzx ' + RN(r, 4) + ', ' + MN(m, size));
244346 case size of
245347 1: if signed then OpM(0, true, $BE0F, r, m) else OpM(0, false, $B60F, r, m);
246348 2: if signed then OpM(0, true, $BF0F, r, m) else OpM(0, false, $B70F, r, m);
@@ -251,6 +353,7 @@
251353
252354 procedure Store(const m: TMem; r: longint; size: longint);
253355 begin
356+ if listing then Say('mov ' + MN(m) + ', ' + RN(r, size));
254357 case size of
255358 1: OpM(0, false, $88, r, m, true);
256359 2: OpM($66, false, $89, r, m);
@@ -261,16 +364,19 @@
261364
262365 procedure Lea(r: longint; const m: TMem);
263366 begin
367+ if listing then Say('lea ' + RN(r) + ', ' + MN(m));
264368 OpM(0, true, $8D, r, m);
265369 end;
266370
267371 procedure Alu(op, dst, src: longint);
268372 begin
373+ if listing then Say(AluNames[op] + ' ' + RN(dst) + ', ' + RN(src));
269374 OpR(0, true, op shl 3 or 1, src, dst);
270375 end;
271376
272377 procedure AluI(op, r: longint; v: longint);
273378 begin
379+ if listing then Say(AluNames[op] + ' ' + RN(r) + ', ' + IntStr(v));
274380 if (v >= -128) and (v <= 127) then begin
275381 OpR(0, true, $83, op, r); Put1(v)
276382 end else begin
@@ -281,44 +387,56 @@
281387 { op r, [m] }
282388 procedure AluM(op, r: longint; const m: TMem);
283389 begin
390+ if listing then Say(AluNames[op] + ' ' + RN(r) + ', ' + MN(m));
284391 OpM(0, true, op shl 3 or 3, r, m);
285392 end;
286393
287394 procedure Imul(dst, src: longint);
288395 begin
396+ if listing then Say('imul ' + RN(dst) + ', ' + RN(src));
289397 OpR(0, true, $AF0F, dst, src);
290398 end;
291399
292400 procedure ImulI(r: longint; v: longint);
293401 begin
402+ if listing then Say('imul ' + RN(r) + ', ' + RN(r) + ', ' + IntStr(v));
294403 OpR(0, true, $69, r, r);
295404 Put4(v);
296405 end;
297406
298407 procedure Shift(op, r: longint);
299408 begin
409+ if listing then Say(ShiftNames[op] + ' ' + RN(r) + ', cl');
300410 OpR(0, true, $D3, op, r);
301411 end;
302412
303413 procedure ShiftI(op, r, n: longint);
304414 begin
415+ if listing then Say(ShiftNames[op] + ' ' + RN(r) + ', ' + IntStr(n));
305416 OpR(0, true, $C1, op, r);
306417 Put1(n);
307418 end;
308419
309420 procedure Unary(ext, r: longint);
310421 begin
422+ if listing then Say(UnaryNames[ext] + ' ' + RN(r));
311423 OpR(0, true, $F7, ext, r);
312424 end;
313425
314426 procedure Cqo;
315427 begin
428+ if listing then Say('cqo');
316429 Put1($48); Put1($99);
317430 end;
318431
319432 { Brings a value of size bytes in r to 64 bits, by its sign or zeros. }
320433 procedure Extend(r, size: longint; signed: boolean);
321434 begin
435+ if not listing then
436+ else if size = 4 then begin
437+ if signed then Say('movsxd ' + RN(r) + ', ' + RN(r, 4)) else Say('mov ' + RN(r, 4) + ', ' + RN(r, 4))
438+ end else if signed then Say('movsx ' + RN(r) + ', ' + RN(r, size))
439+ else Say('movzx ' + RN(r, 4) + ', ' + RN(r, size));
322440 case size of
323441 1: if signed then OpR(0, true, $BE0F, r, r) else OpR(0, false, $B60F, r, r);
324442 2: if signed then OpR(0, true, $BF0F, r, r) else OpR(0, false, $B70F, r, r);
@@ -329,29 +447,35 @@
329447 { r := 1 if the condition holds, else 0 }
330448 procedure SetCC(cc, r: longint);
331449 begin
450+ if listing then Say('set' + CondNames[cc] + ' ' + RN(r, 1));
332451 OpR(0, false, $900F + cc shl 8, 0, r);
452+ if listing then Say('movzx ' + RN(r, 4) + ', ' + RN(r, 1));
333453 OpR(0, false, $B60F, r, r);
334454 end;
335455
336456 procedure TestRR(a, b: longint);
337457 begin
458+ if listing then Say('test ' + RN(a) + ', ' + RN(b));
338459 OpR(0, true, $85, b, a);
339460 end;
340461
341462 procedure Push(r: longint);
342463 begin
464+ if listing then Say('push ' + RN(r));
343465 if r >= 8 then Put1($41);
344466 Put1($50 + r and 7);
345467 end;
346468
347469 procedure Pop(r: longint);
348470 begin
471+ if listing then Say('pop ' + RN(r));
349472 if r >= 8 then Put1($41);
350473 Put1($58 + r and 7);
351474 end;
352475
353476 procedure CallChunk(c: longint);
354477 begin
478+ if listing then Say('call ' + ChunkName(c));
355479 Put1($E8);
356480 AddFixup(pc, c, 0);
357481 Put4(0);
@@ -359,91 +483,117 @@
359483
360484 procedure CallBack(target: longint);
361485 begin
486+ if listing then Say('call');
362487 Put1($E8);
363488 Put4(target - (pc + 4));
364489 end;
365490
366491 procedure CallR(r: longint);
367492 begin
493+ if listing then Say('call ' + RN(r));
368494 OpR(0, false, $FF, 2, r);
369495 end;
370496
371497 procedure CallM(const m: TMem);
372498 begin
499+ if listing then Say('call ' + MN(m));
373500 OpM(0, false, $FF, 2, m);
374501 end;
375502
376-procedure Ret; begin Put1($C3) end;
377-procedure Leave; begin Put1($C9) end;
378-procedure Syscall; begin Put1($0F); Put1($05) end;
379-procedure Mfence; begin Put1($0F); Put1($AE); Put1($F0) end;
380-procedure Ud2; begin Put1($0F); Put1($0B) end;
381-procedure RepMovsb; begin Put1($F3); Put1($A4) end;
382-procedure RepStosb; begin Put1($F3); Put1($AA) end;
383-procedure RepeCmpsb; begin Put1($F3); Put1($A6) end;
503+procedure Ret; begin if listing then Say('ret'); Put1($C3) end;
504+procedure Leave; begin Say('leave'); Put1($C9) end;
505+procedure Syscall; begin Say('syscall'); Put1($0F); Put1($05) end;
506+procedure Mfence; begin Say('mfence'); Put1($0F); Put1($AE); Put1($F0) end;
507+procedure Ud2; begin Say('ud2'); Put1($0F); Put1($0B) end;
508+procedure RepMovsb; begin Say('rep movsb'); Put1($F3); Put1($A4) end;
509+procedure RepStosb; begin Say('rep stosb'); Put1($F3); Put1($AA) end;
510+procedure RepeCmpsb; begin Say('repe cmpsb'); Put1($F3); Put1($A6) end;
384511
385512 procedure LockCmpxchg(const m: TMem; r: longint);
386513 begin
514+ if listing then Say('lock cmpxchg ' + MN(m) + ', ' + RN(r));
387515 Put1($F0);
388516 OpM(0, true, $B10F, r, m);
389517 end;
390518
391519 procedure Xchg(const m: TMem; r: longint);
392520 begin
521+ if listing then Say('xchg ' + MN(m) + ', ' + RN(r));
393522 OpM(0, true, $87, r, m);
394523 end;
395524
396525 procedure LockXadd(const m: TMem; r: longint);
397526 begin
527+ if listing then Say('lock xadd ' + MN(m) + ', ' + RN(r));
398528 Put1($F0);
399529 OpM(0, true, $C10F, r, m);
400530 end;
401531
402532 procedure MovqXR(x, r: longint);
403533 begin
534+ if listing then Say('movq ' + XN(x) + ', ' + RN(r));
404535 OpR($66, true, $6E0F, x, r);
405536 end;
406537
407538 procedure MovqRX(r, x: longint);
408539 begin
540+ if listing then Say('movq ' + RN(r) + ', ' + XN(x));
409541 OpR($66, true, $7E0F, x, r);
410542 end;
411543
412544 procedure MovsdXM(x: longint; const m: TMem);
413545 begin
546+ if listing then Say('movsd ' + XN(x) + ', ' + MN(m));
414547 OpM($F2, false, $100F, x, m);
415548 end;
416549
417550 procedure MovsdMX(const m: TMem; x: longint);
418551 begin
552+ if listing then Say('movsd ' + MN(m) + ', ' + XN(x));
419553 OpM($F2, false, $110F, x, m);
420554 end;
421555
422556 const
423557 RealPfx: array[boolean] of longint = ($F2, $F3);
558+ RealEnd: array[boolean] of string[2] = ('sd', 'ss');
424559
425560 procedure Sse(op, x, y: longint; single: boolean);
561+var s: shortstring;
426562 begin
563+ if listing then begin
564+ case op of
565+ $58: s := 'add' + RealEnd[single];
566+ $59: s := 'mul' + RealEnd[single];
567+ $5C: s := 'sub' + RealEnd[single];
568+ $5E: s := 'div' + RealEnd[single];
569+ else s := 'cvt' + RealEnd[single] + '2' + RealEnd[not single];
570+ end;
571+ Say(s + ' ' + XN(x) + ', ' + XN(y));
572+ end;
427573 OpR(RealPfx[single], false, $0F or op shl 8, x, y);
428574 end;
429575
430576 procedure Ucomis(x, y: longint; single: boolean);
431577 begin
578+ if listing then Say('ucomi' + RealEnd[single] + ' ' + XN(x) + ', ' + XN(y));
432579 if single then OpR(0, false, $2E0F, x, y) else OpR($66, false, $2E0F, x, y);
433580 end;
434581
435582 procedure Cvtsi2s(x, r: longint; single: boolean);
436583 begin
584+ if listing then Say('cvtsi2' + RealEnd[single] + ' ' + XN(x) + ', ' + RN(r));
437585 OpR(RealPfx[single], true, $2A0F, x, r);
438586 end;
439587
440588 procedure Cvtts2si(r, x: longint; single: boolean);
441589 begin
590+ if listing then Say('cvtt' + RealEnd[single] + '2si ' + RN(r) + ', ' + XN(x));
442591 OpR(RealPfx[single], true, $2C0F, r, x);
443592 end;
444593
445594 procedure Btc(r, bit: longint);
446595 begin
596+ if listing then Say('btc ' + RN(r) + ', ' + IntStr(bit));
447597 OpR(0, true, $BA0F, 7, r);
448598 Put1(bit);
449599 end;
@@ -454,6 +604,7 @@
454604
455605 procedure Jmp(var L: longint);
456606 begin
607+ if listing then Say('jmp');
457608 Put1($E9);
458609 Put4(L);
459610 L := pc - 4;
@@ -461,6 +612,7 @@
461612
462613 procedure Jcc(cc: longint; var L: longint);
463614 begin
615+ if listing then Say('j' + CondNames[cc]);
464616 Put1($0F); Put1($80 + cc);
465617 Put4(L);
466618 L := pc - 4;
@@ -468,12 +620,14 @@
468620
469621 procedure JmpBack(target: longint);
470622 begin
623+ if listing then Say('jmp');
471624 Put1($E9);
472625 Put4(target - (pc + 4));
473626 end;
474627
475628 procedure JccBack(cc, target: longint);
476629 begin
630+ if listing then Say('j' + CondNames[cc]);
477631 Put1($0F); Put1($80 + cc);
478632 Put4(target - (pc + 4));
479633 end;
@@ -635,7 +789,7 @@
635789 $90F3, $90, $0B0F, $CC, $F4, $9948, $99, $F0AE0F, $E8AE0F, $F8AE0F);
636790 var
637791 w: shortstring; o: array[0..2] of TOpd; n, i, k, size, pfx, cc: longint;
638- wide, byteOp: boolean; at, start: TPos; v: int64;
792+ wide, byteOp, wasListing: boolean; at, start: TPos; v: int64; first: longint;
639793 names, places, chains: array[0..63] of longint; numLabels: longint;
640794 usedAt: array[0..63] of TPos;
641795
@@ -687,6 +841,7 @@
687841 if w = '' then Mark('expected an instruction');
688842 Next;
689843 if tok = sColon then begin
844+ SayAt(pc, w + ':');
690845 k := Lab(Intern(@w[1], length(w)));
691846 if places[k] >= 0 then MarkAt(at, 'label ' + w + ' is defined twice');
692847 places[k] := pc;
@@ -694,6 +849,9 @@
694849 Next;
695850 continue;
696851 end;
852+ first := pc;
853+ wasListing := listing;
854+ listing := false; { the instruction is said as it is written }
697855 if w = 'lock' then begin
698856 Put1($F0);
699857 w := AsmWord;
@@ -825,10 +983,57 @@
825983 end;
826984 end;
827985 if tok <> sSemicolon then Mark('expected ";"');
986+ listing := wasListing;
987+ SayAt(first, Source(SpanFrom(at)));
828988 Next;
829989 end;
830990 for k := 0 to numLabels - 1 do
831991 if places[k] < 0 then MarkAt(usedAt[k], 'label ' + NameOf(names[k]) + ' isn''t defined');
992+end;
993+
994+{ The listing, raz -S: each live procedure of the module in the order
995+ it was made, each line of source it comes from, and under it the
996+ instructions made for it, at their offsets in the procedure; a jump
997+ within it says where to by its offset too. }
998+procedure List(module: longint);
999+var order: ^longint; n, i, j, k, c, t, line: longint; text: shortstring;
1000+begin
1001+ order := GetMem(numChunks * SizeOf(longint));
1002+ n := 0;
1003+ for c := 0 to numChunks - 1 do
1004+ if chunks^[c].live and (chunks^[c].sec = secText) and (chunks^[c].module = module) then begin
1005+ i := n;
1006+ while (i > 0) and (chunks^[order[i - 1]].start > chunks^[c].start) do begin
1007+ order[i] := order[i - 1];
1008+ dec(i);
1009+ end;
1010+ order[i] := c;
1011+ inc(n);
1012+ end;
1013+ i := 0; j := 0;
1014+ for k := 0 to n - 1 do begin
1015+ c := order[k];
1016+ writeln;
1017+ writeln(ChunkName(c), ':');
1018+ line := -1;
1019+ while (i < numSaid) and (said[i].at < chunks^[c].start) do inc(i);
1020+ while (i < numSaid) and (said[i].at < chunks^[c].start + chunks^[c].size) do begin
1021+ while (j < numLines) and (lines^[j].at <= said[i].at) do begin
1022+ if (lines^[j].chunk = c) and (lines^[j].line <> line) then begin
1023+ line := lines^[j].line;
1024+ writeln(line: 6, ' ', LineText(NameOf(lines^[j].path), line));
1025+ end;
1026+ inc(j);
1027+ end;
1028+ text := said[i].text;
1029+ t := said[i].at;
1030+ if (text = 'jmp') or (text = 'call') then text := text + ' ' + IntStr(t + 5 + Get4At(t + 1) - chunks^[c].start)
1031+ else if (text[1] = 'j') and (pos(' ', text) = 0) then text := text + ' ' + IntStr(t + 6 + Get4At(t + 2) - chunks^[c].start);
1032+ writeln(t - chunks^[c].start: 14, ' ', text);
1033+ inc(i);
1034+ end;
1035+ end;
1036+ FreeMem(order);
8321037 end;
8331038
8341039 end.

test/check.sh (history) modified +6 -0

@@ -92,6 +92,12 @@
9292 pass=$((pass + 1))
9393 fi
9494 done
95+# raz -S: test/list/X.raz lists as X.expect
96+for f in test/list/*.raz; do
97+ want=test/list/$(basename $f .raz).expect
98+ if $raz -S $f 2>&1 | cmp -s $want -; then pass=$((pass + 1))
99+ else echo "FAIL raz -S $f"; $raz -S $f 2>&1 | diff $want - | head; fail=$((fail + 1)); fi
100+done
95101 # the report's version is the one the compiler says it follows
96102 # the version the compiler follows: the report's, or one it names while
97103 # the compiler catches up

test/list/Listing.expect (history) added +73 -0

@@ -0,0 +1,73 @@
1+
2+Listing.twice:
3+ 0 lea rax, [rdi + rdi]
4+ 4 ret
5+
6+Listing.add:
7+ 12 proc add(a, b: I64): I64;
8+ 0 push rbp
9+ 1 mov rbp, rsp
10+ 4 sub rsp, 16
11+ 11 mov [rbp - 8], rdi
12+ 15 mov [rbp - 16], rsi
13+ 14 return a + b * 2;
14+ 19 mov r8, [rbp - 8]
15+ 23 mov r9, [rbp - 16]
16+ 27 shl r9, 1
17+ 31 add r8, r9
18+ 34 mov rax, r8
19+ 37 jmp 49
20+ 15 end add;
21+ 42 xor eax, eax
22+ 44 movq xmm0, rax
23+ 49 leave
24+ 50 ret
25+
26+Listing.main:
27+ 17 proc main*;
28+ 0 push rbp
29+ 1 mov rbp, rsp
30+ 4 sub rsp, 48
31+ 11 xor eax, eax
32+ 13 mov [rbp - 8], rax
33+ 20 for i := 1 to 3 do
34+ 17 mov rax, 1
35+ 22 mov [rbp - 8], rax
36+ 26 mov rax, 3
37+ 31 mov [rbp - 16], rax
38+ 35 mov r8, [rbp - 8]
39+ 39 mov r9, [rbp - 16]
40+ 43 cmp r8, r9
41+ 46 jg 173
42+ 21 if i # 2 then total := add(total, twice(i)); end;
43+ 52 mov r8, [rbp - 8]
44+ 56 cmp r8, 2
45+ 60 setne r8b
46+ 64 movzx r8d, r8b
47+ 68 test r8, r8
48+ 71 je 135
49+ 77 mov r8, [Listing.total]
50+ 84 mov [rbp - 24], r8
51+ 88 mov r8, [rbp - 8]
52+ 92 mov [rbp - 32], r8
53+ 96 mov rdi, [rbp - 32]
54+ 100 call Listing.twice
55+ 105 mov r8, rax
56+ 108 mov [rbp - 40], r8
57+ 112 mov rdi, [rbp - 24]
58+ 116 mov rsi, [rbp - 40]
59+ 120 call Listing.add
60+ 125 mov r8, rax
61+ 128 mov [Listing.total], r8
62+ 135 mov r8, [rbp - 16]
63+ 139 mov r9, [rbp - 8]
64+ 143 sub r8, r9
65+ 146 cmp r8, 1
66+ 150 jb 173
67+ 156 mov r8, [rbp - 8]
68+ 160 add r8, 1
69+ 164 mov [rbp - 8], r8
70+ 168 jmp 35
71+ 23 end main;
72+ 173 leave
73+ 174 ret

test/list/Listing.raz (history) added +25 -0

@@ -0,0 +1,25 @@
1+module Listing;
2+(* raz -S: each line of source, and the instructions made for it *)
3+
4+var total: I64;
5+
6+proc twice(x: I64): I64;
7+code
8+ lea rax, [rdi + rdi];
9+ ret;
10+end twice;
11+
12+proc add(a, b: I64): I64;
13+begin
14+ return a + b * 2;
15+end add;
16+
17+proc main*;
18+var i: I64;
19+begin
20+ for i := 1 to 3 do
21+ if i # 2 then total := add(total, twice(i)); end;
22+ end;
23+end main;
24+
25+end Listing.