raz / commit ed43cacf

commit
ed43cacfc4ac6cb7b88152e3794233e6b396cc13
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
c5489654
Self: scan as fast as the seed; jump without a jmp reg

A table for the symbols that are one byte each (83 ms to 75, the
seed's), jump as push and ret, and the README says what the code
generator could still do: match as a jump table, faster slice equality.

patch · log

3 files changed, +36 -24

self/Jump.raz modified +2 -1

@@ -49,7 +49,8 @@
4949 mov r15, [rdi + 48];
5050 mov rcx, [rdi + 56];
5151 mov eax, 1;
52- db 0FFH, 0E1H; (* jmp rcx, which the assembler has no form for *)
52+ push rcx; (* a jump to rcx, for which the assembler has no form: *)
53+ ret; (* push the place, and return to it *)
5354 end jump;
5455
5556 end Jump.

self/README.md modified +17 -7

@@ -26,18 +26,28 @@
2626 `werror`, `recovery`, `skipping`) have setters. The `sbuf` array,
2727 which can't be sliced outside, is `strBytes`.
2828 - **Going on after an error.** raz has no `setjmp`: `Jump.raz` is the
29- two in assembly, which the parser's recovery needs. The assembler has
30- no `jmp reg`, so it is written `db 0FFH, 0E1H;`.
29+ two in assembly. The parser's 200 or so error sites are deep in its
30+ recursion and all end in a jump back to the statement or declaration
31+ being read, which is how the seed reports up to 20 errors in a run
32+ (15 of the 267 tests of messages show more than one). The assembler
33+ has no `jmp reg`, so `jump` is `push rcx; ret`.
3134 - **`NO_COLOR` set to nothing.** `Sys.env` gives "" for a variable that
3235 is unset and for one that is empty, so the port reads the second as
33- the first, as no-color.org has it. The seed reads it as set.
36+ the first. no-color.org says a variable that is empty doesn't turn
37+ colour off, so the port is the one that follows it and the seed
38+ differs; no test depends on either.
3439 - **Strings are slices.** A message is a `[]U8` made by `Diag.say`,
3540 which keeps what it makes: messages are made only to be shown. A name
3641 from `nameOf` stays valid, since names are kept in chunks that are
3742 never moved; the seed's was a copy.
3843 - **No bit cast.** A real's bits and back go through a pointer.
3944 - **Speed.** On the 348,025 lines of `test/genbench.py`, 7.6 MB, the
40- seed's scanner takes 75 ms and this one 83 ms. Two things cost the
41- rest: a slice comparison, `a[0..n] = b[0..n]`, is 25% slower than the
42- same bytes in a loop, and a procedure call to ask whether a byte is
43- in a name (a table now). The compiler could mend the first.
45+ seed's scanner takes 75 ms and this one 74. It took 121 at first.
46+ What closed the gap, in the source: a byte loop in place of the slice
47+ comparison `a[0..n] = b[0..n]` (97 ms), a 256-entry table for the
48+ bytes of a name (84), and one for the symbols that are a byte each
49+ (75). What is left for the code generator, which would help every
50+ module: a `match` on an integer is a chain of compares, each loading
51+ the selector from the frame and jumping over a `jmp` (`je L1; jmp L2`),
52+ where FPC makes a jump table; and the slice comparison is slower than
53+ the loop it stands for.

self/Scan.raz modified +17 -16

@@ -12,7 +12,7 @@
1212 not closed: the first such error stops the compiler. The parser goes
1313 on after its own errors, through recovery below. *)
1414 module Scan;
15-import Sys, Heap, Mem, Fmt, Diag, Jump;
15+import Sys, Heap, Mem, Fmt, Str, Diag, Jump;
1616
1717 type
1818 NamePtr = ^U8;
@@ -60,6 +60,8 @@
6060 "+", "-", "*", "/", "=", "#", "<", "<=", ">", ">=",
6161 ":=", ":", ";", ",", ".", "..", "...",
6262 "(", ")", "[", "]", "{", "}", "^", "&", "~", "|", "_"];
63+ (* the operators that no longer one starts, so that one byte is the symbol *)
64+ alone = "+-*/#;,()[]{}^~";
6365 PoolChunk = 65536;
6466
6567 var
@@ -81,6 +83,10 @@
8183 since the loop that reads a name asks it of every byte. *)
8284 nameChar: [256]BOOL;
8385
86+ (* The symbol a byte is by itself, as + is, or sNull: a table, since
87+ most symbols are of this kind and a match would test them one by one. *)
88+ single: [256]Sym;
89+
8490 cur: State; (* buf .. f; tok, ival, rval and id live above *)
8591
8692 (* The name table: every distinct identifier once, found through an
@@ -870,6 +876,11 @@
870876 cur.tokPos := cur.pos;
871877 cur.tokLine := cur.line;
872878 cur.tokLineStart := cur.lineStart;
879+ if single[c] # sNull then
880+ INC(cur.pos);
881+ tok := single[c];
882+ return;
883+ end;
873884 match c of
874885 | 'A'..'Z', 'a'..'z', '_':
875886 start := cur.pos;
@@ -915,10 +926,6 @@
915926 INC(cur.pos);
916927 sbuf[slen] := 0;
917928 tok := sString;
918- | '+': INC(cur.pos); tok := sPlus;
919- | '-': INC(cur.pos); tok := sMinus;
920- | '*': INC(cur.pos); tok := sTimes;
921- | '/': INC(cur.pos); tok := sSlash;
922929 | '=':
923930 if cur.buf[cur.pos + 1] = '=' then
924931 slip(2, sEql, "comparing is written =, not ==");
@@ -933,7 +940,6 @@
933940 INC(cur.pos);
934941 tok := sAmp;
935942 end;
936- | '#': INC(cur.pos); tok := sNeq;
937943 | '<':
938944 INC(cur.pos);
939945 if cur.buf[cur.pos] = '=' then INC(cur.pos); tok := sLeq; else tok := sLss; end;
@@ -943,8 +949,6 @@
943949 | ':':
944950 INC(cur.pos);
945951 if cur.buf[cur.pos] = '=' then INC(cur.pos); tok := sBecomes; else tok := sColon; end;
946- | ';': INC(cur.pos); tok := sSemicolon;
947- | ',': INC(cur.pos); tok := sComma;
948952 | '.':
949953 INC(cur.pos);
950954 if cur.buf[cur.pos] = '.' then
@@ -953,13 +957,6 @@
953957 else
954958 tok := sPeriod;
955959 end;
956- | '(': INC(cur.pos); tok := sLparen;
957- | ')': INC(cur.pos); tok := sRparen;
958- | '[': INC(cur.pos); tok := sLbrak;
959- | ']': INC(cur.pos); tok := sRbrak;
960- | '{': INC(cur.pos); tok := sLbrace;
961- | '}': INC(cur.pos); tok := sRbrace;
962- | '^': INC(cur.pos); tok := sArrow;
963960 | '|':
964961 if cur.buf[cur.pos + 1] = '|' then
965962 slip(2, sOr, "\"or\" is written or, not ||");
@@ -967,7 +964,6 @@
967964 INC(cur.pos);
968965 tok := sBar;
969966 end;
970- | '~': INC(cur.pos); tok := sTilde;
971967 | '!':
972968 if cur.buf[cur.pos + 1] = '=' then
973969 slip(2, sNeq, "\"not equal\" is written #, not !=");
@@ -987,6 +983,11 @@
987983
988984 begin
989985 for i := 0 to 255 do nameChar[i] := isLetter(U8(i)) or isDigit(U8(i)); end;
986+ for i := 0 to NumOps - 1 do
987+ if (LEN(ops[i]) = 1) and (Str.find(alone, ops[i]) >= 0) then
988+ single[ops[i][0]] := Sym(INT(sPlus) + i);
989+ end;
990+ end;
990991 chunkLeft := 0;
991992 nameCap := 1 shl 12;
992993 namePtr := ^NamePtr(grab(nameCap * SIZE(NamePtr)));