From 23f6bd0ddf5b5bee0226e8ebff8cd8aefc78308e Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Fri, 16 Oct 2026 11:36:34 +0100 Subject: [PATCH] Drop seed-check, by the owner's word src/ and the seed have grown apart: the dumper pairs that compared them unit by unit, src/check.sh and its list of files the seed can't read, go. make fixpoint and make seed-bench stay; test/Recover.raz runs in make check. continue takes its alphabetical place among the reserved words, which kept the seed's numbers only for those dumpers. --- diff --git a/CLAUDE.md b/CLAUDE.md index 424a555..2620756 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -15,7 +15,7 @@ make kernels # test/bench's kernels against FPC -O2's twins make loc # lines of the compiler, by rloc; the last, code, is the budget make tests # the small Pascal tests of scan, x64, link and elf - make seed-check # src/ against the seed: scan to x64, then the messages and statuses of every program + make seed-bench # the scanner's and the compiler's speed against the seed's make fixpoint # the seed builds src, that builds src, that again: the last two agree make gdb # gdb stops at a line of a test and names it make stress # 40,000 native threads; make check runs a small version @@ -135,8 +135,10 @@ `src/` is the compiler, in raz: one module to a seed unit, the same names. `seed/`, the Free Pascal compiler it was ported from, is the **seed**: frozen at its last version, kept to build -`src/` the first time and as the reference its units are tested -against, and to be dropped in time. Never edit the seed; a difference +`src/` the first time (`make raz`, `make fixpoint`), and to be dropped +in time. `src/` is no longer tested against it unit by unit (the owner +dropped `seed-check` after v0.17.0): `make check`, the fixpoint and +`conform/` are its tests. Never edit the seed; a difference found is told to the owner. `sbcl/` is an example, not part of the build: a second compiler, in @@ -237,9 +239,6 @@ is written with its type; shared with anything else, the name alone is that other declaration. A `match` arm on an enumeration takes the type's names alone or with the type, which must be the matched one. -- `continue` is the last reserved word, so that the others keep the - seed's numbers; the seed's dumpers intern it first - (`src/test/seedcontinue.pas`). - `Width` follows glibc's `wcwidth` but gives a control 0, and 0 to the format characters glibc counts as 1 (Egyptian hieroglyph controls, interlinear annotation), but for the prepended concatenation marks. diff --git a/Makefile b/Makefile index afa952a..9de5564 100644 --- a/Makefile +++ b/Makefile @@ -47,10 +47,7 @@ check: raz build/raz-lib.3 test/check.sh -# the compiler's units, and the whole compiler, tested against the seed's -seed-check: raz | build - src/check.sh - +# the scanner's and the compiler's speed against the seed's seed-bench: raz | build src/bench.sh @@ -184,4 +181,4 @@ site: raz pdf python3 site/build.py -.PHONY: units tests seed-check seed-bench fixpoint check gdb bench kernels stress fmt loc man pdf dist site clean install uninstall +.PHONY: units tests seed-bench fixpoint check gdb bench kernels stress fmt loc man pdf dist site clean install uninstall diff --git a/doc/compiler.md b/doc/compiler.md index 7a520ac..97fee8b 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -14,7 +14,7 @@ | File | Lines | Code | What | |---|---|---|---| | `src/Diag.raz` | 410 | 315 | Messages: positions, source excerpts, colour, counts | -| `src/Scan.raz` | 1,127 | 934 | Symbols, literals, comments, positions | +| `src/Scan.raz` | 1,125 | 934 | Symbols, literals, comments, positions | | `src/Sym.raz` | 707 | 566 | Types, objects, scopes, layout | | `src/Chunk.raz` | 363 | 279 | The compiled program: chunks, fixups, line notes | | `src/X64.raz` | 1,517 | 1,253 | The instruction encoder, jumps within a procedure, the assembler | @@ -1769,7 +1769,7 @@ instruction, or of calls to gen right there. - **An instruction:** a routine in `X64.raz` built on `opM` or `opR`, and a line in `test/enctest.pas` to check its encoding (the seed's - test; `src/test/X64Dump.raz` is the port's). + test), and a program in `conform/` or `test/` that uses it. - **A machine:** a new `Gen.raz`, answering the same calls, and an encoder and assembler for it in place of `X64.raz`. - **A statement:** a reserved word in `Scan.raz` (in `Sym` and `words`, @@ -1783,12 +1783,7 @@ to suit `src/`); a raz that already exists could do it as well. `make fixpoint` has the seed's build compile `src/`, that compile it again, and that again, and checks that the last two are the same file. -`make seed-check` tests each unit of `src/` against the seed's. The -seed has no `continue`, the last reserved word of `src/Scan.raz`, so -the Pascal dumpers in `src/test` use `seedcontinue`, which interns the -name right after the seed's reserved words: every name then has the -same number on both sides, and only the end of a file's symbols, one -higher in `src/`, is renumbered (`ScanDump.raz`'s `seedSym`). A file -that uses something the seed doesn't know is left out (`src/check.sh`'s -`newer`). After any change: `make check`, `make bench`, +`make seed-bench` compares the speed of the two. Each unit of `src/` +was once tested against the seed's, while it was ported; that test was +dropped after v0.17.0, as the two had grown apart. After any change: `make check`, `make bench`, `make loc`. diff --git a/src/README.md b/src/README.md index 872e005..408f254 100644 --- a/src/README.md +++ b/src/README.md @@ -6,42 +6,19 @@ rules: plain code, each module opening with what it is, what it depends on and what it leaves out. The seed is never edited from here. - make seed-check # each unit against the seed's, on the same files - make seed-bench # the scanner's speed against the seed's + make seed-bench # the scanner's and the compiler's speed against the seed's make fixpoint # the seed builds src, that builds src, that builds it again - -`Chunk` and `Sym` have no input files to read, so each has a dumper -pair, `test/ChunkDump` and `test/SymDump`, that runs one fixed -pseudo-random script of calls on its side and prints all it made or -answered (`Sym` also ends, as an argument says, in the error of a name -declared twice or of too many scopes): the output, the messages and -the status must be the same. `Link` and `Elf` are tested by -`test/LinkDump`, which makes a program of chunks by a script and writes -it as a static, a dynamic and a stripped executable, which must be the -same file; `X64` by `test/X64Dump`, every encoder with operands from a -script, bytes, fixups and listing, and by `test/AsmDump` on the files -`test/genasm.py` makes: a long run of every instruction the assembler -knows, and a file for each of its errors. -`Gen` and `Item` were tested by `test/GenDump`, a script of calls, valid -by construction, that compiles 80 procedures with every kind of -expression, statement, call, check, frame and defer, and the entry -point, and by `test/ItemDump`, a script of operands and operations with -mistakes among them, over 60 scripts (the text its messages point into -is `test/ItemDump.txt`): the bytes, fixups, line notes and listing, and -the messages, had to be the same as the seed's. They were, until the -compiler's code was improved (v0.10.1): `src/` makes better code than -the seed, so the dumpers are kept, as a way to read what `Gen` makes, -and no longer compared. `src/check.sh` says what is. -`Parse` and `Raz`, the last two units, are tested whole: the seed builds -`src/Raz.raz` into `build/src/raz.src`, and each compiler, run as -`build/cmp/raz` (a program holds the paths it was made from, and the -library is found beside the compiler), compiles the library, the -examples, the tools, `src/` itself, every test of `test/` and -`conform/`, and the 348,025-line program of `test/genbench.py`, with the -options each test has. The messages and the status must be the same -for every file (the executables no longer are, since v0.10.1): 409 of them, 289 of which are errors. -`make seed-bench` gives the speed of both. +While it was ported, each unit was tested against the seed's: a pair +of dumpers, one over each side's units, read the same files or ran the +same pseudo-random script of calls, and had to print the same, with +the same messages and status; the compiler built whole had to give +every program the messages and status the seed gave. That test was +dropped after v0.17.0: the two had grown apart, the compiler making +better code since v0.10.1 and following newer reports since, and +`make check`, `conform/` and the fixpoint test the compiler as it is. +`test/Recover.raz`, the pattern of going on after an error, is run by +`make check`. `make fixpoint` (`fixpoint.sh`) is the proof that the port is whole: the seed builds `Raz.raz` (stage 1), stage 1 builds it (stage 2), and @@ -50,13 +27,6 @@ than the seed's). Stage 3 then stands in for `./raz`, and `make check` and `make bench` run on it. Each stage is built by the same command in the same place, since a program holds the paths it was made from. - -`check.sh` builds a dumper for each side, `test/ScanDump.pas` over the -seed's units and `test/ScanDump.raz` over these, and has both read the -library, the examples, every test of the compiler and a file for each -message the scanner makes (`test/genlex.py`) and for hundreds of reals -that are hard to read exactly (`test/genreals.py`): the output, the -messages and the status must be the same. ## What the port found diff --git a/src/Scan.raz b/src/Scan.raz index 7748752..6202b00 100644 --- a/src/Scan.raz +++ b/src/Scan.raz @@ -22,14 +22,12 @@ sBecomes, sColon, sSemicolon, sComma, sPeriod, sUpto, sEllipsis, sLparen, sRparen, sLbrak, sRbrak, sLbrace, sRbrace, sArrow, sAmp, sTilde, sBar, sBlank, - (* the reserved words, in the order of words below: alphabetical - but for continue, which came last (report 2026-10.31), so that - the others keep the numbers the seed gives them *) - sAnd, sBegin, sBreak, sBy, sCode, sConst, sDefer, sDiv, sDo, sElse, + (* the reserved words, in the order of words below *) + sAnd, sBegin, sBreak, sBy, sCode, sConst, sContinue, sDefer, sDiv, sDo, sElse, sElsif, sEnd, sExtern, sFor, sForward, sIf, sImport, sIn, sLink, sMatch, sMod, sModule, sNot, sOf, sOr, sPacked, sProc, sRecord, sRepeat, sReturn, sSet, sShl, sShr, sThen, sTo, sTry, sType, sUntil, sVar, - sVariant, sVolatile, sWhile, sXor, sContinue, + sVariant, sVolatile, sWhile, sXor, sEof); (* Where the scanner is. Importing a module compiles it in the middle @@ -53,12 +51,12 @@ NumWords = 44; NumOps = 28; words: [NumWords][]U8 = [ - "and", "begin", "break", "by", "code", "const", "defer", "div", "do", + "and", "begin", "break", "by", "code", "const", "continue", "defer", "div", "do", "else", "elsif", "end", "extern", "for", "forward", "if", "import", "in", "link", "match", "mod", "module", "not", "of", "or", "packed", "proc", "record", "repeat", "return", "set", "shl", "shr", "then", "to", "try", "type", "until", "var", "variant", "VOLATILE", "while", - "xor", "continue"]; + "xor"]; (* sPlus to sBlank *) ops: [NumOps][]U8 = [ "+", "-", "*", "/", "=", "#", "<", "<=", ">", ">=", diff --git a/src/bench.sh b/src/bench.sh index e3150f4..5d211f3 100755 --- a/src/bench.sh +++ b/src/bench.sh @@ -1,9 +1,10 @@ #!/bin/sh # The scanner's speed against the seed's, on the compiler's own # benchmark program: the best of seven runs of each. Run from the -# repository root, after src/check.sh has built the tests. +# repository root, after make raz. set -e b=build/src +mkdir -p $b python3 test/genbench.py 12000 >$b/Bench.raz fpc -O2 -Mobjfpc -Sh- -vew -FUbuild -Fuseed -o$b/scanbench.seed src/test/ScanBench.pas >/dev/null ./raz -Werror -I src -o $b/scanbench.src src/test/ScanBench.raz diff --git a/src/check.sh b/src/check.sh deleted file mode 100755 index febd57e..0000000 --- a/src/check.sh +++ /dev/null @@ -1,190 +0,0 @@ -#!/bin/sh -# Tests src/'s units against the seed's: each dumper reads the same -# files and must give the same standard output, standard error and -# status. Run from the repository root, after `make raz`; the seed's -# side is built by fpc from seed/. -set -e -b=build/src -# The seed predates raw strings and match on a string (report -# 2026-10.25): the files that use them are the compiler's alone, and -# so are those whose message says what match takes. It predates, too, -# an INT widened in an expression and the rest (report 2026-10.26), -# the messages for an I64 that goes to an INT, and a help that -# converts the narrower of two integers; and views, v.(T), -# whose tests are conform/*/View*.raz, and which Math, examples/Elf.raz -# and, through Math, test/bench/NBody.raz use; and returning a slice of -# the frame as an error (report 2026-10.27); an array constant typed -# []T (report 2026-10.29); [] as the empty slice, seen through a view -# (report 2026-10.30); and U8, I8 and I16 widening to INT, continue, -# and enumerations' names with their types (report 2026-10.31). -newer="conform/run/RawStrings.raz conform/err/ScanRawOpen.raz conform/err/ScanRawIndent.raz - conform/run/MatchStr.raz conform/err/MatchStrTwice.raz conform/err/MatchStrNoElse.raz - conform/err/MatchStrVar.raz conform/err/MatchReal.raz conform/err/AtMatch.raz - examples/Sh.raz examples/Settings.raz - conform/run/IntWiden.raz conform/err/IntIncNarrow.raz conform/err/IntNarrow.raz conform/err/IntNarrowSum.raz - conform/err/IntResultsNarrow.raz conform/err/GenIntTypes.raz - lib/Math.raz examples/Elf.raz test/bench/NBody.raz - conform/err/FrameSlice.raz - conform/run/ArrayConstLen.raz conform/err/ArrayConstLenEmpty.raz conform/err/ArrayConstLenLiteral.raz - conform/run/EmptySlice.raz - conform/run/SmallWiden.raz conform/err/SmallWidenMade.raz conform/err/SmallWidenNeg.raz - conform/err/SmallWidenI64.raz conform/err/SmallWidenMadeOp.raz - conform/run/Continue.raz conform/err/ContinueOutside.raz conform/err/ContinueDefer.raz - conform/err/ContinueName.raz - conform/run/EnumNames.raz conform/err/EnumShared.raz conform/err/EnumNoName.raz - conform/err/EnumArmType.raz conform/err/EnumSharedImported.raz conform/err/EnumTwiceOther.raz - tools/Widths.raz lib/Width.raz" -newer() { - case $1 in conform/*/View*.raz) return 0 ;; esac - for g in $newer; do [ $g = $1 ] && return 0; done - return 1 -} -mkdir -p $b -fpc -O2 -Mobjfpc -Sh- -vew -FUbuild -Fuseed -o$b/scandump.seed src/test/ScanDump.pas >/dev/null -./raz -Werror -I src -o $b/scandump.src src/test/ScanDump.raz -python3 src/test/genreals.py > $b/reals.raz -# one small file for each message the scanner makes, and the places -# diag shows them in: a tab, a long line, characters before the place -mkdir -p $b/lex -python3 src/test/genlex.py $b/lex -n=0; bad=0 -for f in $b/lex/*.raz lib/*.raz examples/*.raz tools/*.raz src/*.raz src/test/*.raz test/run/*.raz test/err/*.raz \ - conform/*/*.raz $b/reals.raz; do - [ -f "$f" ] || continue - newer $f && continue - s1=0; s2=0 - $b/scandump.seed $f >$b/seed.out 2>$b/seed.err || s1=$? - $b/scandump.src $f >$b/src.out 2>$b/src.err || s2=$? - n=$((n + 1)) - if [ $s1 != $s2 ] || ! cmp -s $b/seed.out $b/src.out || ! cmp -s $b/seed.err $b/src.err; then - echo "differs: $f (status $s1 and $s2)"; bad=$((bad + 1)) - fi -done -echo "$n files, $bad differ" -test $bad = 0 - -# Jump: a parser's way of going on after an error -./raz -Werror -I src -o $b/recover src/test/Recover.raz -printf 'a bad b bad bad c\nd bad\n' >$b/rec.raz -r=$($b/recover $b/rec.raz 2>/dev/null) || test $? = 1 -test "$r" = "8 names, 4 failed, 4 errors" && echo "recover ok" - -# Chunk and Sym: a fixed pseudo-random script each, run by both sides, -# which print all they have made or answered; Sym also ends, as the -# argument says, in an error, whose message and status must agree -for u in Chunk Sym; do - l=$(echo $u | tr A-Z a-z) - fpc -O2 -Mobjfpc -Sh- -vew -FUbuild -Fuseed -o$b/${l}dump.seed src/test/${u}Dump.pas >/dev/null - ./raz -Werror -I src -o $b/${l}dump.src src/test/${u}Dump.raz -done -for a in "" dup deep; do - for s in seed src; do - t=0; $b/chunkdump.$s >$b/chunk.$s 2>/dev/null || t=$? - t2=0; $b/symdump.$s $a >$b/sym.$s.out 2>$b/sym.$s.err || t2=$? - echo "$t $t2" >$b/status.$s - done - cmp $b/status.seed $b/status.src - cmp $b/chunk.seed $b/chunk.src - cmp $b/sym.seed.out $b/sym.src.out - cmp $b/sym.seed.err $b/sym.src.err -done -echo "chunk and sym ok" - -# Link and Elf: a program of chunks made by a script, linked and written -# as a static, a dynamic and a stripped executable, which must be the -# same file from both sides -fpc -O2 -Mobjfpc -Sh- -vew -FUbuild -Fuseed -o$b/linkdump.seed src/test/LinkDump.pas >/dev/null -./raz -Werror -I src -o $b/linkdump.src src/test/LinkDump.raz -for m in 0 1 2; do - $b/linkdump.seed $m $b/link.seed - $b/linkdump.src $m $b/link.src - cmp $b/link.seed $b/link.src -done -echo "link and elf ok" - -# X64: every encoder, then the assembler on a long run of instructions -# of every kind and a file for each error it makes; the bytes and the -# listing, or the message and status, must be the same -fpc -O2 -Mobjfpc -Sh- -vew -FUbuild -Fuseed -o$b/x64dump.seed src/test/X64Dump.pas >/dev/null -./raz -Werror -I src -o $b/x64dump.src src/test/X64Dump.raz -$b/x64dump.seed >$b/x64.seed -$b/x64dump.src >$b/x64.src -cmp $b/x64.seed $b/x64.src -fpc -O2 -Mobjfpc -Sh- -vew -FUbuild -Fuseed -o$b/asmdump.seed src/test/AsmDump.pas >/dev/null -./raz -Werror -I src -o $b/asmdump.src src/test/AsmDump.raz -mkdir -p $b/asm -python3 src/test/genasm.py $b/asm -n=0; bad=0 -for f in $b/asm/*.raz; do - s1=0; s2=0 - $b/asmdump.seed $f >$b/seed.out 2>$b/seed.err || s1=$? - $b/asmdump.src $f >$b/src.out 2>$b/src.err || s2=$? - n=$((n + 1)) - if [ $s1 != $s2 ] || ! cmp -s $b/seed.out $b/src.out || ! cmp -s $b/seed.err $b/src.err; then - echo "differs: $f (status $s1 and $s2)"; bad=$((bad + 1)) - fi -done -echo "x64: $n assembler files, $bad differ" -test $bad = 0 - -# Gen and Item were tested here against the seed's while they were ported, -# by the code they made: the same bytes, listings and line notes. Since the -# compiler's own code was improved (v0.10.1) that code is no longer the -# seed's, so they are tested by what the seed can still check, the -# messages and statuses of Parse and Raz below, and by the fixpoint and -# the tests of `make check`, on the compiler they make. - -# Parse and Raz: the compiler this makes, built by the seed, compiles -# what the seed compiles, with the same messages and status (the files -# are no longer the same, as it makes better code): every example, test and tool, and the library, with each -# one's own options. Both sides run as build/cmp/raz, since a program -# holds the paths it was made from and the library is found beside the -# compiler. -printf 'module Version;\nconst Text* = "%s";\nend Version.\n' "$(tr -d "'" < build/version.inc)" >$b/Version.raz -./raz -Werror -I src -I $b -o $b/raz.src src/Raz.raz -c=build/cmp -mkdir -p $c -ln -sfn ../../lib $c/lib -# and the big program that times the compiler, 348,025 lines -mkdir -p $c/bench -python3 test/genbench.py 12000 >$c/bench/Bench.raz -files= -for f in $c/bench/Bench.raz lib/*.raz examples/*.raz tools/*.raz src/*.raz test/run/*.raz test/libc/*.raz test/bench/*.raz \ - conform/run/*.raz test/err/*.raz conform/err/*.raz; do - newer $f || files="$files $f" -done -for side in seed src; do - rm -rf $c/$side - mkdir -p $c/$side - if [ $side = seed ]; then cp build/seed/raz $c/raz; else cp $b/raz.src $c/raz; fi - for f in $files; do - dir=$(dirname $f); name=$(basename $f .raz); x=$(echo $dir | tr / _).$name - flags= - case $dir in - examples|tools|lib|src) flags=-Werror ;; - esac - [ -f $dir/$name.args ] && [ $dir = test/err ] && flags="$flags $(cat $dir/$name.args)" - case $dir in - lib) opts= ;; - src|tools) opts="-I src -I build/src" ;; - test/err) opts="-I test/err/mods -I test/run/mods" ;; - *) opts="-I $dir/mods" ;; - esac - s=0 - $c/raz $flags $opts -o $c/$side/$x $f >$c/$side/$x.out 2>$c/$side/$x.err || s=$? - echo $s >$c/$side/$x.status - done -done -n=0; bad=0 -for f in $files; do - x=$(dirname $f | tr / _).$(basename $f .raz) - n=$((n + 1)) - for e in .out .err .status; do - [ -f $c/seed/$x$e ] || [ -f $c/src/$x$e ] || continue - if ! cmp -s $c/seed/$x$e $c/src/$x$e; then - echo "differs: $f ($x$e)"; bad=$((bad + 1)); break - fi - done -done -echo "raz: $n files, $bad differ" -test $bad = 0 diff --git a/src/test/AsmDump.pas b/src/test/AsmDump.pas deleted file mode 100644 index 0f539c7..0000000 --- a/src/test/AsmDump.pas +++ /dev/null @@ -1,46 +0,0 @@ -{ Assembles the instructions in the file of the argument, as the body of - a code procedure that ends in end, and prints the bytes and the - listing: the seed's side of a test of the assembler in src/X64.raz, - whose AsmDump.raz does the same and prints the same. Constants are - numbers, with a minus sign, and sums of them. } -program asmdump; -{$mode objfpc}{$H-} -uses diag, scan, seedcontinue, chunk, x64; -var i: longint; path: string; - -function ConstVal(whole: boolean): int64; -var neg: boolean; v: int64; -begin - neg := tok = sMinus; - if neg then Next; - if tok <> sInt then Mark('expected a number'); - v := ival; - Next; - if neg then v := -v; - while whole and (tok in [sPlus, sMinus]) do begin - neg := tok = sMinus; - Next; - if tok <> sInt then Mark('expected a number'); - if neg then v := v - ival else v := v + ival; - Next; - end; - ConstVal := v; -end; - -begin - if Open(ParamStr(1)) <> '' then Fatal('no file'); - path := 'lib/Rt.raz'; - curFile := Intern(@path[1], length(path)); - listing := true; - NewChunk(secText, 0, 0); - BeginChunk(0); - NoteLine(1, 1); - Next; - Assemble(@ConstVal); - EndChunk(0); - chunks^[0].live := true; - for i := 0 to pc - 1 do write(HexStr(code^[i], 2)); - writeln; - List(0); - Finish; -end. diff --git a/src/test/AsmDump.raz b/src/test/AsmDump.raz deleted file mode 100644 index 61cda72..0000000 --- a/src/test/AsmDump.raz +++ /dev/null @@ -1,55 +0,0 @@ -(* Assembles the instructions in the file of the argument, as the body of - a code procedure that ends in end, and prints the bytes and the - listing: src/X64.raz's assembler's side of a test against the seed's, - AsmDump.pas, which prints the same. Constants are numbers, with a - minus sign, and sums of them. *) -module AsmDump; -import Out, Files, Diag, Scan, Chunk, X64; - -proc number(neg: BOOL): (I64, ERROR); -var v: I64; -begin - if Scan.tok # Scan.sInt then return 0, Scan.mark("expected a number"); end; - v := Scan.ival; - Scan.next; - if neg then v := -v; end; - return v, nil; -end; - -proc constant(whole: BOOL): (I64, ERROR); -var neg: BOOL; v, w: I64; -begin - neg := Scan.tok = Scan.sMinus; - if neg then Scan.next; end; - v := try number(neg); - while whole and ((Scan.tok = Scan.sPlus) or (Scan.tok = Scan.sMinus)) do - neg := Scan.tok = Scan.sMinus; - Scan.next; - w := try number(neg); - v := v + w; - end; - return v, nil; -end; - -proc main*; -var i: INT; path: []U8; err: ERROR; -begin - if LEN(Scan.open(Files.arg(1))) # 0 then Diag.fatal("no file"); end; - path := "lib/Rt.raz"; - Chunk.setCurFile(Scan.intern(ADR(path[0]), LEN(path))); - X64.setListing(true); - _ := Chunk.newChunk(Chunk.secText, 0, 0); - Chunk.beginChunk(0); - Chunk.noteLine(1, 1); - Scan.next; - err := X64.assemble(constant); - if err # nil then Diag.finish; end; - Chunk.endChunk(0); - Chunk.setLive(0); - for i := 0 to Chunk.pc - 1 do Out.hex(U64(Chunk.text[i]), 2); end; - Out.ln; - X64.list(0); - Diag.finish; -end; - -end AsmDump. diff --git a/src/test/ChunkDump.pas b/src/test/ChunkDump.pas deleted file mode 100644 index b7bf112..0000000 --- a/src/test/ChunkDump.pas +++ /dev/null @@ -1,89 +0,0 @@ -{ Makes a program of chunks by a fixed pseudo-random script and prints - all of it: the seed's side of a test of src/Chunk.raz, whose - ChunkDump.raz does the same and prints the same. } -program chunkdump; -{$mode objfpc}{$H-} -uses diag, scan, seedcontinue, chunk; -var - seed: int64; - names: array[0..7] of longint; - i, k, n, op, c, sum, open, x, y, z: longint; - b: array[0..63] of byte; - -function Rnd(n: longint): longint; -begin - seed := (seed * 1103515245 + 12345) and $7FFFFFFF; - Rnd := (seed shr 8) mod n; -end; - -begin - seed := 7; - for i := 0 to 7 do begin b[0] := 97 + i; names[i] := Intern(@b[0], 1) end; - open := -1; - sum := 0; - for k := 1 to 30000 do begin - op := Rnd(16); - case op of - 0..3: Put1(Rnd(256)); - 4: begin x := Rnd(1 shl 30); y := Rnd(1 shl 30); Put4(x - y) end; - 5: begin x := Rnd(1 shl 30); y := Rnd(1 shl 30); Put8(int64(x) * 4096 - y) end; - 6: begin - if open >= 0 then EndChunk(open); - x := Rnd(5); y := Rnd(9); - open := NewChunk(secText, x - 1, y - 1); - BeginChunk(open); - end; - 7: begin x := Rnd(8); curFile := names[x]; x := Rnd(300); y := Rnd(80); NoteLine(x, y) end; - 8: if numChunks > 0 then begin x := Rnd(numChunks); y := Rnd(100); AddFixup(pc, x, y - 50) end; - 9: if (open >= 0) and (pc - chunks^[open].start > 8) then begin x := Rnd(6); TakeBack(x) end; - 10: begin - x := Rnd(5); y := Rnd(9); - c := NewChunk(secRodata, x - 1, y - 1); - n := Rnd(64); - for i := 0 to n - 1 do b[i] := Rnd(256); - x := Rnd(4); - SetData(c, @b[0], n, 1 shl x); - n := Rnd(4); - for i := 1 to n do begin x := Rnd(8); y := Rnd(numChunks); z := Rnd(10); AddDataFixup(c, x * 8, y, z) end; - end; - 11: begin - x := Rnd(5); y := Rnd(9); - c := NewChunk(secBss, x - 1, y - 1); - x := Rnd(100); y := Rnd(4); - SetBss(c, x, 1 shl y); - end; - 12: begin x := Rnd(8); c := ExternChunk(names[x]) end; - 13: if numChunks > 0 then begin - x := Rnd(numChunks); y := Rnd(8); z := Rnd(200); n := Rnd(5); - NoteVar(x, names[y], z - 100, n); - end; - 14: if pc >= 8 then begin - x := Rnd(1 shl 30); y := Rnd(1 shl 30); - Put4At(pc - 8, x - y); - sum := (sum + Get4At(pc - 8)) and $FFFFFF; - end; - 15: if numChunks > 0 then begin - x := Rnd(numChunks); chunks^[x].live := true; - x := Rnd(numChunks); y := Rnd(1 shl 30); chunks^[x].adr := y; - end; - end; - end; - if open >= 0 then EndChunk(open); - writeln('sum ', sum, ' pc ', pc, ' rolen ', rolen); - for i := 0 to numChunks - 1 do - with chunks^[i] do - writeln('C ', i, ' ', ord(sec), ' ', start, ' ', size, ' ', align, ' ', firstFix, ' ', numFix, ' ', - module, ' ', name, ' ', adr, ' ', ord(live), ' ', ord(ext), ' ', ChunkName(i)); - for i := 0 to numFixups - 1 do - writeln('F ', fixups^[i].at, ' ', fixups^[i].chunk, ' ', fixups^[i].addend); - for i := 0 to numDataFixups - 1 do - writeln('D ', dataFixups^[i].at, ' ', dataFixups^[i].chunk, ' ', dataFixups^[i].addend); - for i := 0 to numLines - 1 do - writeln('L ', lines^[i].at, ' ', lines^[i].chunk, ' ', lines^[i].path, ' ', lines^[i].line, ' ', lines^[i].col); - for i := 0 to numVarNotes - 1 do - writeln('V ', varNotes[i].chunk, ' ', varNotes[i].name, ' ', varNotes[i].off, ' ', varNotes[i].kind); - for i := 0 to pc - 1 do write(HexStr(code^[i], 2)); - writeln; - for i := 0 to rolen - 1 do write(HexStr(rodata^[i], 2)); - writeln; -end. diff --git a/src/test/ChunkDump.raz b/src/test/ChunkDump.raz deleted file mode 100644 index 30a025c..0000000 --- a/src/test/ChunkDump.raz +++ /dev/null @@ -1,118 +0,0 @@ -(* Makes a program of chunks by a fixed pseudo-random script and prints - all of it: src/Chunk.raz's side of a test against the seed's, - ChunkDump.pas, which prints the same. *) -module ChunkDump; -import Out, Fmt, Scan, Chunk; - -var seed: I64; - -proc rnd(n: INT): INT; -begin - seed := (seed * 1103515245 + 12345) & 7FFFFFFFH; - return INT((seed shr 8) mod I64(n)); -end; - -(* a flag, as the number the seed's side prints for it *) -proc bit(f: BOOL): INT; -begin - if f then return 1; end; - return 0; -end; - -proc main*; -var - names: [8]INT; b: [64]U8; - i, k, n, op, c, sum, open, x, y, z: INT; -begin - seed := 7; - for i := 0 to 7 do b[0] := U8(97 + i); names[i] := Scan.intern(ADR(b[0]), 1); end; - open := -1; - sum := 0; - for k := 1 to 30000 do - op := rnd(16); - match op of - | 0..3: Chunk.put1(rnd(256)); - | 4: x := rnd(1 shl 30); y := rnd(1 shl 30); Chunk.put4(x - y); - | 5: x := rnd(1 shl 30); y := rnd(1 shl 30); Chunk.put8(I64(x) * 4096 - I64(y)); - | 6: - if open >= 0 then Chunk.endChunk(open); end; - x := rnd(5); y := rnd(9); - open := Chunk.newChunk(Chunk.secText, x - 1, y - 1); - Chunk.beginChunk(open); - | 7: - x := rnd(8); Chunk.setCurFile(names[x]); - x := rnd(300); y := rnd(80); Chunk.noteLine(x, y); - | 8: - if Chunk.numChunks > 0 then - x := rnd(Chunk.numChunks); y := rnd(100); Chunk.addFixup(Chunk.pc, x, y - 50); - end; - | 9: - if (open >= 0) and (Chunk.pc - Chunk.chunks[open].start > 8) then - x := rnd(6); Chunk.takeBack(x); - end; - | 10: - x := rnd(5); y := rnd(9); - c := Chunk.newChunk(Chunk.secRodata, x - 1, y - 1); - n := rnd(64); - for i := 0 to n - 1 do b[i] := U8(rnd(256)); end; - x := rnd(4); - Chunk.setData(c, ADR(b[0]), n, 1 shl x); - n := rnd(4); - for i := 1 to n do - x := rnd(8); y := rnd(Chunk.numChunks); z := rnd(10); - Chunk.addDataFixup(c, x * 8, y, z); - end; - | 11: - x := rnd(5); y := rnd(9); - c := Chunk.newChunk(Chunk.secBss, x - 1, y - 1); - x := rnd(100); y := rnd(4); - Chunk.setBss(c, x, 1 shl y); - | 12: x := rnd(8); c := Chunk.externChunk(names[x]); - | 13: - if Chunk.numChunks > 0 then - x := rnd(Chunk.numChunks); y := rnd(8); z := rnd(200); n := rnd(5); - Chunk.noteVar(x, names[y], z - 100, n); - end; - | 14: - if Chunk.pc >= 8 then - x := rnd(1 shl 30); y := rnd(1 shl 30); - Chunk.put4At(Chunk.pc - 8, x - y); - sum := (sum + Chunk.get4At(Chunk.pc - 8)) & 0FFFFFFH; - end; - | else: - if Chunk.numChunks > 0 then - x := rnd(Chunk.numChunks); Chunk.setLive(x); - x := rnd(Chunk.numChunks); y := rnd(1 shl 30); Chunk.setAdr(x, y); - end; - end; - end; - if open >= 0 then Chunk.endChunk(open); end; - Fmt.print("sum {} pc {} rolen {}\n", [Fmt.int(sum), Fmt.int(Chunk.pc), Fmt.int(Chunk.rolen)]); - for i := 0 to Chunk.numChunks - 1 do - Fmt.print("C {} {} {} {} {} {} {} {} {} {} {} {} {}\n", [Fmt.int(i), Fmt.int(INT(Chunk.chunks[i].sec)), - Fmt.int(Chunk.chunks[i].start), Fmt.int(Chunk.chunks[i].size), Fmt.int(Chunk.chunks[i].align), - Fmt.int(Chunk.chunks[i].firstFix), Fmt.int(Chunk.chunks[i].numFix), Fmt.int(Chunk.chunks[i].home), - Fmt.int(Chunk.chunks[i].name), Fmt.int(Chunk.chunks[i].adr), - Fmt.int(bit(Chunk.chunks[i].live)), Fmt.int(bit(Chunk.chunks[i].ext)), Fmt.str(Chunk.chunkName(i))]); - end; - for i := 0 to Chunk.numFixups - 1 do - Fmt.print("F {} {} {}\n", [Fmt.int(Chunk.fixups[i].at), Fmt.int(Chunk.fixups[i].chunk), Fmt.int(Chunk.fixups[i].addend)]); - end; - for i := 0 to Chunk.numDataFixups - 1 do - Fmt.print("D {} {} {}\n", [Fmt.int(Chunk.dataFixups[i].at), Fmt.int(Chunk.dataFixups[i].chunk), Fmt.int(Chunk.dataFixups[i].addend)]); - end; - for i := 0 to Chunk.numLines - 1 do - Fmt.print("L {} {} {} {} {}\n", [Fmt.int(Chunk.lines[i].at), Fmt.int(Chunk.lines[i].chunk), - Fmt.int(Chunk.lines[i].path), Fmt.int(Chunk.lines[i].line), Fmt.int(Chunk.lines[i].col)]); - end; - for i := 0 to Chunk.numVarNotes - 1 do - Fmt.print("V {} {} {} {}\n", [Fmt.int(Chunk.varNotes[i].chunk), Fmt.int(Chunk.varNotes[i].name), - Fmt.int(Chunk.varNotes[i].off), Fmt.int(Chunk.varNotes[i].kind)]); - end; - for i := 0 to Chunk.pc - 1 do Out.hex(U64(Chunk.text[i]), 2); end; - Out.ln; - for i := 0 to Chunk.rolen - 1 do Out.hex(U64(Chunk.rodata[i]), 2); end; - Out.ln; -end; - -end ChunkDump. diff --git a/src/test/GenDump.pas b/src/test/GenDump.pas deleted file mode 100644 index b64d148..0000000 --- a/src/test/GenDump.pas +++ /dev/null @@ -1,708 +0,0 @@ -{ Compiles procedures made by a fixed pseudo-random script of calls to - Gen, valid by construction, then prints the bytes, the fixups and the - listing (raz -S) of what it made: the seed's side of a test of - src/Gen.raz, whose GenDump.raz makes and prints the same. With an - argument, the entry point is the dynamic one. } -program gendump; -{$mode objfpc}{$H-} -uses diag, scan, seedcontinue, sym, chunk, x64, gen; - -const - FirstProc = 3; - NumProcs = 80; - FirstGlob = FirstProc + NumProcs; - NumGlobs = 4; - Arith: array[0..9] of TSym = (sPlus, sMinus, sTimes, sDiv, sMod, sAmp, sBar, sXor, sShl, sShr); - Cmps: array[0..5] of TSym = (sEql, sNeq, sLss, sLeq, sGtr, sGeq); - RealOps: array[0..3] of TSym = (sPlus, sMinus, sTimes, sSlash); - -var - seed: int64; - slots: array[0..5] of int64; - regs: array[0..4] of longint; - numRegs: longint; - xregs: array[0..3] of longint; - numX: longint; - tuple2, tuple3, record3, sliceT: PType; - results: array[0..8] of PType; - curRes: longint; - -function Rnd(n: longint): longint; -begin - seed := (seed * 1103515245 + 12345) and $7FFFFFFF; - Rnd := (seed shr 8) mod n; -end; - -function Pos: TPos; -var p: TPos; -begin - p.f := 0; p.a := Rnd(400); p.b := p.a + 1; - Pos := p; -end; - -function Size: longint; -begin - Size := 1 shl Rnd(4); -end; - -function Slot: int64; -begin - Slot := slots[Rnd(6)]; -end; - -function Bit(f: boolean): longint; -begin - if f then Bit := 1 else Bit := 0; -end; - -function Flag: boolean; -begin - Flag := Rnd(2) = 0; -end; - -function ConstVal: int64; -var x: longint; r: int64; -begin - x := Rnd(1 shl 30); - case Rnd(7) of - 0: r := x mod 10; - 1: r := -(x mod 100); - 2: r := int64(1) shl (x mod 20); - 3: r := x; - 4: r := int64(x) * 4096 * 4096 * 9 - 7; - 5: r := -int64(x) * 8; - else r := x mod 1000 - 500; - end; - ConstVal := r; -end; - -function NewObjs(t: PType; n: longint): PObj; -var first, o, last: PObj; i: longint; -begin - first := nil; last := nil; - for i := 0 to n - 1 do begin - o := NewObj(oVar, 0, t); - o^.val := 8 * i; - if last = nil then first := o else last^.next := o; - last := o; - end; - NewObjs := first; -end; - -procedure MakeTypes; -begin - tuple2 := NewType(fRecord, 16, 8); - tuple2^.tuple := true; tuple2^.len := 2; tuple2^.fields := NewObjs(tInt, 2); - tuple3 := NewType(fRecord, 24, 8); - tuple3^.tuple := true; tuple3^.len := 3; tuple3^.fields := NewObjs(tInt, 3); - record3 := NewType(fRecord, 24, 8); - record3^.fields := NewObjs(tReal, 3); - sliceT := NewType(fSlice, 16, 8); - sliceT^.base := tU8; - results[0] := nil; - results[1] := tInt; - results[2] := tReal; - results[3] := sliceT; - results[4] := tuple2; - results[5] := tuple3; - results[6] := tF32; - results[7] := tI16; - results[8] := record3; -end; - -function MemAdr: TAdr; -var a: TAdr; c: longint; -begin - c := Rnd(3); - if c = 0 then begin - a.base := bFrame; a.chunk := 0; a.hi := 0; a.off := Slot; if Flag then a.off := a.off + 8; - end else if c = 1 then begin - a.base := bGlobal; a.chunk := FirstGlob + Rnd(NumGlobs); a.hi := 0; a.off := 8 * Rnd(3); - end else begin - a.base := bSlot; a.chunk := Slot; a.hi := 0; a.off := 8 * Rnd(3); - end; - MemAdr := a; -end; - -function PushableAdr: TAdr; -var a: TAdr; -begin - a.chunk := 0; a.hi := 0; - if Flag then begin - a.base := bFrame; a.off := Slot; if Flag then a.off := a.off + 8; - end else begin - a.base := bGlobal; a.chunk := FirstGlob + Rnd(NumGlobs); a.off := 8 * Rnd(3); - end; - PushableAdr := a; -end; - -function RegAdr(r: longint): TAdr; -var a: TAdr; -begin - a.base := bReg; a.chunk := r; a.hi := 0; a.off := 0; - RegAdr := a; -end; - -function StackAdr: TAdr; -var a: TAdr; -begin - a.base := bStack; a.chunk := 0; a.hi := 0; a.off := 8 * Rnd(3); - StackAdr := a; -end; - -procedure PushIntVar; -var a: TAdr; sz, c: longint; -begin - sz := Size; - c := Rnd(5); - if (c = 0) and (numRegs > 0) then begin - if Flag then inPlace := regs[Rnd(numRegs)]; - PushVar(RegAdr(regs[Rnd(numRegs)]), 8, false, kWord); - end else if c = 1 then begin - PushAdr(PushableAdr); - a := StackAdr; - PushVar(a, sz, Flag, kWord); - end else begin - a := MemAdr; - PushVar(a, sz, Flag, kWord); - end; -end; - -procedure IntExpr(b: longint); forward; -procedure RealExpr(k: TKind; b: longint); forward; -procedure CallExpr(want, b: longint); forward; - -function RealKind: TKind; -begin - if Flag then RealKind := kF64 else RealKind := kF32; -end; - -procedure IntExpr(b: longint); -var c, sz, n: longint; sg, f: boolean; k: TKind; sy: TSym; a: TAdr; -begin - if b > 0 then c := Rnd(14) else c := Rnd(3); - case c of - 0: PushConst(kWord, ConstVal); - 1: PushIntVar; - 2: PushIntVar; - 3, 4, 5: begin - IntExpr(b - 1); IntExpr(b - 1); - sy := Arith[Rnd(10)]; sz := Size; sg := Flag; - Op(sy, kWord, sz, sg); - end; - 6: - if Flag then begin - IntExpr(b - 1); IntExpr(b - 1); - sy := Cmps[Rnd(6)]; sg := Flag; - Compare(sy, kWord, sg); - end else begin - k := RealKind; - RealExpr(k, b - 1); RealExpr(k, b - 1); - sy := Cmps[Rnd(6)]; - Compare(sy, k, false); - end; - 7: begin - IntExpr(b - 1); - n := Rnd(4); sz := Size; sg := Flag; - if n = 0 then Neg(kWord, sz, sg) - else if n = 1 then Complement(sz, sg) - else if n = 2 then NotBool - else Narrow(sz, sg); - end; - 8: begin - IntExpr(b - 1); - a := PushableAdr; - sz := Size; sg := Flag; - n := 10 + Rnd(100); - CheckIndex(n, Pos); - IndexArray(a, sz); - a := StackAdr; a.off := 0; - PushVar(a, sz, sg, kWord); - end; - 9: begin - n := Rnd(3); - sz := Size; sg := Flag; - if n = 0 then begin - a := MemAdr; - if a.base = bGlobal then a.base := bFrame; - IntExpr(b - 1); - f := Flag; - IndexSlice(a, sz, f, Pos); - end else if n = 1 then begin - PushAdr(PushableAdr); - IntExpr(b - 1); - a := StackAdr; a.off := 0; - f := Flag; - IndexSlice(a, sz, f, Pos); - end else begin - a.base := bReg; a.chunk := R13; a.hi := R14; a.off := 0; - IntExpr(b - 1); - f := Flag; - IndexSlice(a, sz, f, Pos); - end; - a := StackAdr; a.off := 0; - PushVar(a, sz, sg, kWord); - end; - 10: begin - k := RealKind; - RealExpr(k, b - 1); - RealToInt(k); - end; - 11: CallExpr(1, b - 1); - 12: begin - IntExpr(b - 1); - PushConst(kWord, Rnd(100) - 50); - Op(sPlus, kWord, 8, false); - end; - else begin - IntExpr(b - 1); - PushConst(kWord, ConstVal); - sy := Arith[Rnd(10)]; sz := Size; sg := Flag; - Op(sy, kWord, sz, sg); - end; - end; -end; - -procedure RealExpr(k: TKind; b: longint); -var c, n: longint; sy: TSym; a: TAdr; k2: TKind; v: int64; -begin - if b > 0 then c := Rnd(9) else c := Rnd(3); - case c of - 0: begin - n := Rnd(4); - if n = 0 then v := $3FF0000000000000 - else if n = 1 then v := $4000000000000000 - else if n = 2 then v := 0 - else v := $3FE0000000000000; - PushConst(k, v); - end; - 1: begin - a := MemAdr; - if k = kF32 then PushVar(a, 4, false, k) else PushVar(a, 8, false, k); - end; - 2: - if numX > 0 then begin - if Flag then inPlace := xregs[Rnd(numX)]; - PushVar(RegAdr(xregs[Rnd(numX)]), 8, false, k); - end else begin - PushAdr(PushableAdr); - a := StackAdr; - PushVar(a, 8, false, k); - end; - 3, 4: begin - RealExpr(k, b - 1); RealExpr(k, b - 1); - sy := RealOps[Rnd(4)]; - Op(sy, k, 8, false); - end; - 5: begin - IntExpr(b - 1); - IntToReal(k, Flag); - end; - 6: begin - RealExpr(k, b - 1); - Neg(k, 8, false); - end; - 7: begin - if k = kF32 then k2 := kF64 else k2 := kF32; - RealExpr(k2, b - 1); - RealToReal(k2); - end; - else CallExpr(2, b - 1); - end; -end; - -procedure CallExpr(want, b: longint); -var - c: TCall; sig: PType; i, n, kind, r, ni: longint; k: TKind; -begin - sig := NewType(fProc, 8, 8); - sig^.variadic := Rnd(5) = 0; - if want = 1 then begin - r := Rnd(5); - if r = 0 then sig^.base := tBool - else if r = 1 then sig^.base := tI8 - else if r = 2 then sig^.base := tU32 - else if r = 3 then sig^.base := tuple2 - else sig^.base := tInt; - end else if want = 2 then begin - if Flag then sig^.base := tReal else sig^.base := tF32; - end else - sig^.base := results[Rnd(9)]; - c.sig := sig; c.param := nil; c.obj := nil; c.ext := Rnd(4) = 0; - c.n := 0; c.target := 0; - c.chunk := FirstProc + Rnd(NumProcs); - if Rnd(5) = 0 then begin c.chunk := -1; c.ext := false; c.target := Slot end; - n := Rnd(8); - for i := 0 to n - 1 do begin - kind := Rnd(4); - if kind = 0 then begin - k := RealKind; - RealExpr(k, b); - c.isF[c.n] := true; c.pair[c.n] := false; - inc(c.n); - end else if kind = 1 then begin - IntExpr(b); IntExpr(b); - c.isF[c.n] := false; c.pair[c.n] := true; - c.isF[c.n + 1] := false; c.pair[c.n + 1] := false; - inc(c.n, 2); - end else begin - IntExpr(b); - c.isF[c.n] := false; c.pair[c.n] := false; - inc(c.n); - end; - end; - Call(c); - ni := 0; - if sig^.base <> nil then begin - if sig^.base^.form = fSlice then ni := 0 - else if sig^.base = tuple2 then ni := 2 - else if BehindPointer(sig^.base) then ni := 0 - else ni := 1; - end; - if want = 0 then begin - for i := 1 to ni do Drop; - end else if ni = 2 then - Drop; -end; - -procedure BoolExpr(b: longint); -var sy: TSym; sg: boolean; -begin - if Flag then begin - IntExpr(b); IntExpr(b); - sy := Cmps[Rnd(6)]; sg := Flag; - Compare(sy, kWord, sg); - end else - IntExpr(b); -end; - -procedure Stmt(b: longint); forward; - -procedure Assign(b: longint); -var a: TAdr; c, sz: longint; k: TKind; -begin - c := Rnd(6); - sz := Size; - if c = 0 then begin - a := MemAdr; - IntExpr(b); - PopVar(a, sz); - end else if c = 1 then begin - PushAdr(PushableAdr); - a := StackAdr; - IntExpr(b); - PopVar(a, sz); - end else if (c = 2) and (numRegs > 0) then begin - a := RegAdr(regs[Rnd(numRegs)]); - inPlace := a.chunk; - PushVar(a, 8, false, kWord); - IntExpr(b); - Op(Arith[Rnd(3)], kWord, 8, false); - PopVar(a, 8); - end else if (c = 3) and (numX > 0) then begin - k := RealKind; - a := RegAdr(xregs[Rnd(numX)]); - RealExpr(k, b); - PopVar(a, 8); - end else if c = 4 then begin - k := RealKind; - a := MemAdr; - RealExpr(k, b); - if k = kF32 then PopVar(a, 4) else PopVar(a, 8); - end else begin - a := MemAdr; - PushConst(kWord, ConstVal); - PopVar(a, sz); - end; -end; - -procedure RetStmt(b: longint); -var i: longint; -begin - if curRes = 1 then IntExpr(b) - else if curRes = 7 then IntExpr(b) - else if curRes = 2 then RealExpr(kF64, b) - else if curRes = 6 then RealExpr(kF32, b) - else if (curRes = 3) or (curRes = 4) then begin IntExpr(b); IntExpr(b) end - else if curRes = 5 then begin - for i := 0 to 2 do begin IntExpr(b); ResultTo(8 * i, 8) end; - end else if curRes = 8 then begin - PushAdr(PushableAdr); - CopyResult(0, 24); - end; - ReturnJump; -end; - -procedure Stmt(b: longint); -var - c, n, lo, i, start, saved, sz, tmp: longint; L, E, S, fj, tj: longint; a, a2: TAdr; sy: TSym; sg: boolean; - mk: int64; f: boolean; -begin - mk := TempMark; - if b <= 0 then c := Rnd(4) else c := Rnd(22); - case c of - 0, 1, 2, 3: Assign(b); - 4: begin - L := NoLabel; - BoolExpr(b); - JumpIf(L, false); - Stmt(b - 1); - Fix(L); - end; - 5: begin - L := NoLabel; - AlignLoop; - start := pc; - BoolExpr(b); - JumpIf(L, false); - Stmt(b - 1); - JumpBack(start); - Fix(L); - end; - 6: begin - L := NoLabel; - IntExpr(b); IntExpr(b); - sy := Cmps[Rnd(6)]; sg := Flag; - if Flag then JumpCmp(sy, sg, L) else JumpOut(sy, sg, L); - Stmt(b - 1); - Fix(L); - end; - 7: begin - fj := NoLabel; tj := NoLabel; - IntExpr(b); IntExpr(b); - sy := Cmps[Rnd(6)]; sg := Flag; - CondValue(sy, sg, fj, tj); - a := MemAdr; - PopVar(a, 1); - end; - 8: CallExpr(0, b); - 9: begin - a := MemAdr; - if a.base = bSlot then begin a.base := bFrame; a.off := slots[0] end; - IntExpr(b); - PopVar(a, 8); - E := NoLabel; - n := 2 + Rnd(4); lo := Rnd(5) - 2; - if Flag then S := NoLabel else S := pc; - start := JumpTable(a.off, lo, n, S, E); - for i := 0 to n - 1 do TableEntry(start, pc + 4 * (n - i)); - for i := 0 to n - 1 do - Stmt(b - 1); - Fix(E); - end; - 10: begin - PushAdr(PushableAdr); PushAdr(PushableAdr); - n := 1 + Rnd(150); lo := 8 * Rnd(3); - CopyBlock(n, lo); - end; - 11: begin - PushAdr(PushableAdr); - a := MemAdr; - if Flag and (numRegs > 1) then begin - a.base := bReg; a.chunk := regs[0]; a.hi := regs[1]; a.off := 0; - end; - n := 1 + Rnd(150); - CopyTo(a, n); - end; - 12: begin - a := MemAdr; - n := 1 + Rnd(100); - ZeroVar(a, n); - end; - 13: begin - tmp := AllocTemp(16, 8); - n := Rnd(3); - sz := Size; sg := Flag; - if n = 0 then begin - a := PushableAdr; - PushAdr(a); IntExpr(b); IntExpr(b); - a := StackAdr; a.off := 0; - f := Flag; n := 10 + Rnd(50); - Slice(a, f, n, sz, tmp, sg, Pos); - end else if n = 1 then begin - a := MemAdr; - if a.base = bGlobal then a.base := bFrame; - IntExpr(b); IntExpr(b); - Slice(a, false, 0, sz, tmp, sg, Pos); - end else begin - IntExpr(b); IntExpr(b); IntExpr(b); - SlicePtr(sz, tmp, sg, Pos); - end; - end; - 14: begin - a.off := Slot; a2.off := Slot; sz := Size; f := Flag; - CopySlice(a.off, a2.off, sz, f, Pos); - end; - 15: begin - n := Rnd(4); sz := Size; sg := Flag; - if n = 0 then begin - PushAdr(PushableAdr); IntExpr(b); - f := Flag; - IncDec(sz, sg, f); - end else if (n = 1) and (numRegs > 0) then begin - IntExpr(b); - IncDecReg(regs[0], sz, sg, Flag); - end else if n = 2 then begin - IntExpr(b); - n := 3 + Rnd(6); lo := FirstGlob + Rnd(NumGlobs); - CheckEnum(n, lo, Pos); - Drop; - end else begin - IntExpr(b); - n := Rnd(8000); - CheckNil(n, Pos); - Drop; - end; - end; - 16: begin - n := Rnd(3); - if n = 0 then begin - a.off := Slot; a2.off := Slot; n := Rnd(4); f := Flag; - CompareSeq(a.off, a2.off, n, f); - Drop; - end else if n = 1 then begin - IntExpr(b); - ErrorText(AllocTemp(16, 8)); - end else begin - IntExpr(b); - n := 1 + Rnd(200); - ZeroNew(n); - Drop; - end; - end; - 17: begin - n := Rnd(3); - if n = 0 then begin - IntExpr(b); IntExpr(b); IntExpr(b); - Atomic(bCas); - end else if n = 1 then begin - IntExpr(b); IntExpr(b); - Atomic(bXchg); - end else begin - IntExpr(b); IntExpr(b); - Atomic(bXadd); - end; - Drop; - end; - 18: begin - n := Rnd(7); - for i := 1 to n do IntExpr(b); - SysCall(n); - Drop; - if Flag then Fence; - end; - 19: begin - L := NoLabel; - start := DeferBegin(L, saved); - Stmt(b - 1); - DeferEnd(L, saved); - RunDefer(start); - end; - 20: if curRes <> 0 then RetStmt(b) else Assign(b); - else - if curRes <> 0 then begin - TryAlign; - TryReturn(Slot); - end else - Assign(b); - end; - ReleaseTemps(mk); -end; - -procedure GenProc(c: longint); -var - sig: PType; params, p, last: PObj; i, n, kind, mask, j, ns, line: longint; t: PType; - mk: TCodeMark; open: boolean; -begin - sig := NewType(fProc, 8, 8); - curRes := Rnd(9); - sig^.base := results[curRes]; - params := nil; last := nil; - n := Rnd(8); - for i := 0 to n - 1 do begin - kind := Rnd(5); - if kind = 0 then t := tReal - else if kind = 1 then t := sliceT - else if kind = 2 then t := tI32 - else t := tInt; - p := NewObj(oVar, 0, t); - p^.param := true; - p^.isVar := kind = 4; - if last = nil then params := p else last^.next := p; - last := p; - end; - BeginProc(sig, params); - for i := 0 to 5 do slots[i] := AllocPerm(8, 8); - mask := Rnd(32); - j := Rnd(16); - mask := mask or (j shl XBit); - if Rnd(3) = 0 then mask := 0; - numRegs := 0; numX := 0; - for j := 0 to 4 do - if mask and (1 shl j) <> 0 then begin regs[numRegs] := VarRegs[j]; inc(numRegs) end; - for j := 0 to 3 do - if mask and (1 shl (XBit + j)) <> 0 then begin - xregs[numX] := XVarRegs[j]; inc(numX); - XHome(XVarRegs[j], AllocPerm(8, 8)); - end; - j := 0; - p := params; - while p <> nil do begin - if (p^.typ = tInt) and (not p^.isVar) and (j < numRegs) and Flag then begin - p^.reg := regs[j]; inc(j); - end else if (p^.typ = tReal) and (numX > 0) and Flag then begin - p^.reg := xregs[0]; - end else if Flag then - KeepArrived(p); - p := p^.next; - end; - UseRegs(mask); - line := 1 + Rnd(40); - EnterBody(c, line); - MarkCode(mk); - if Flag then begin - Stmt(2); - Rewind(mk); - Redo(mk); - end; - ns := 2 + Rnd(8); - for i := 1 to ns do Stmt(3); - open := Flag and (sig^.base <> nil); - if (sig^.base <> nil) and (not open) then RetStmt(2); - EndProc(c, open); - writeln('proc ', c, ' lost ', Bit(lost), ' regs ', mask, ' saves ', chunks^[c].saveMask, ' at ', chunks^[c].saveAt); -end; - -function ChunkNameOf(i: longint): longint; -var s: string; -begin - s := 'p' + chr(48 + i div 10) + chr(48 + i mod 10); - ChunkNameOf := Intern(@s[1], 3); -end; - -var - i, c, entry: longint; path: string; links: array[0..1] of TLink; dyn: boolean; -begin - seed := 5; - path := 'lib/Rt.raz'; - if Open(path) <> '' then Fatal('no file'); - dyn := ParamCount >= 1; - listing := true; - curFile := Intern(@path[1], length(path)); - for i := 0 to NumProcs - 1 do begin c := NewChunk(secText, -1, ChunkNameOf(i)); chunks^[c].live := true end; - for i := 0 to NumGlobs - 1 do begin - c := NewChunk(secBss, -1, ChunkNameOf(NumProcs + i)); - SetBss(c, 64, 8); - end; - MakeTypes; - for i := 0 to NumProcs - 1 do GenProc(FirstProc + i); - links[0].chunk := FirstProc; links[0].into := FirstGlob; - links[1].chunk := FirstProc + 1; links[1].into := FirstGlob + 1; - entry := EntryPoint(dyn, nil, nil, FirstProc + 2, FirstProc + 3, links, 2); - writeln('entry ', entry, ' pc ', pc, ' fixups ', numFixups, ' lines ', numLines, ' said ', SaidCount); - for i := 0 to pc - 1 do write(HexStr(code^[i], 2)); - writeln; - for i := 0 to numFixups - 1 do writeln('F ', fixups^[i].at, ' ', fixups^[i].chunk, ' ', fixups^[i].addend); - for i := 0 to numLines - 1 do writeln('L ', lines^[i].at, ' ', lines^[i].chunk, ' ', lines^[i].line, ' ', lines^[i].col); - List(-1); -end. diff --git a/src/test/GenDump.raz b/src/test/GenDump.raz deleted file mode 100644 index 0f4af1c..0000000 --- a/src/test/GenDump.raz +++ /dev/null @@ -1,732 +0,0 @@ -(* Compiles procedures made by a fixed pseudo-random script of calls to - Gen, valid by construction (an expression pushes its operands, then - operates on them; a statement leaves the stack as it found it), then - prints the bytes, the fixups, the line notes and the listing (raz -S) - of what it made: src/Gen.raz's side of a test against the seed's, - GenDump.pas, which makes and prints the same. With the argument dyn, - the entry point is the dynamic one. *) -module GenDump; -import Out, Fmt, Diag, Scan, Sym, Chunk, X := X64, Gen; - -const - FirstProc = 3; (* chunks 0 to 2 are Gen's own *) - NumProcs = 80; - FirstGlob = FirstProc + NumProcs; - NumGlobs = 4; - Arith: [10]Scan.Sym = [Scan.sPlus, Scan.sMinus, Scan.sTimes, Scan.sDiv, Scan.sMod, - Scan.sAmp, Scan.sBar, Scan.sXor, Scan.sShl, Scan.sShr]; - Cmps: [6]Scan.Sym = [Scan.sEql, Scan.sNeq, Scan.sLss, Scan.sLeq, Scan.sGtr, Scan.sGeq]; - RealOps: [4]Scan.Sym = [Scan.sPlus, Scan.sMinus, Scan.sTimes, Scan.sSlash]; - -var - seed: I64; - slots: [6]I64; (* frame slots of the procedure *) - regs: [5]INT; (* the registers of variables it keeps *) - numRegs: INT; - xregs: [4]INT; (* ... of reals, as numbered from XmmVar *) - numX: INT; - tuple2, tuple3, record3, sliceT: ^Sym.Type; - results: [9]^Sym.Type; - curRes: INT; (* the procedure's result: an index of results *) - -proc rnd(n: INT): INT; -begin - seed := (seed * 1103515245 + 12345) & 7FFFFFFFH; - return INT((seed shr 8) mod I64(n)); -end; - -proc pos: Diag.Pos; -var p: Diag.Pos; -begin - p.f := 0; p.a := rnd(400); p.b := p.a + 1; - return p; -end; - -(* a size of 1, 2, 4 or 8 bytes *) -proc size: INT; -begin - return 1 shl rnd(4); -end; - -(* a slot of the procedure's frame *) -proc slot: I64; -begin - return slots[rnd(6)]; -end; - -proc bit(f: BOOL): INT; -begin - if f then return 1; end; - return 0; -end; - -proc flag: BOOL; -begin - return rnd(2) = 0; -end; - -proc constVal: I64; -var x: INT; r: I64; -begin - x := rnd(1 shl 30); - match rnd(7) of - | 0: r := x mod 10; - | 1: r := -(x mod 100); - | 2: r := I64(1) shl (x mod 20); - | 3: r := x; - | 4: r := I64(x) * 4096 * 4096 * 9 - 7; - | 5: r := -I64(x) * 8; - | else: r := x mod 1000 - 500; - end; - return r; -end; - -proc newObjs(t: ^Sym.Type; n: INT): ^Sym.Obj; -var first, o, last: ^Sym.Obj; i: INT; -begin - first := nil; last := nil; - for i := 0 to n - 1 do - o := Sym.newObj(Sym.oVar, 0, t); - o^.val := 8 * i; - if last = nil then first := o; else last^.next := o; end; - last := o; - end; - return first; -end; - -proc makeTypes; -begin - tuple2 := Sym.newType(Sym.fRecord, 16, 8); - tuple2^.tuple := true; tuple2^.len := 2; tuple2^.fields := newObjs(Sym.tInt, 2); - tuple3 := Sym.newType(Sym.fRecord, 24, 8); - tuple3^.tuple := true; tuple3^.len := 3; tuple3^.fields := newObjs(Sym.tInt, 3); - record3 := Sym.newType(Sym.fRecord, 24, 8); - record3^.fields := newObjs(Sym.tReal, 3); - sliceT := Sym.newType(Sym.fSlice, 16, 8); - sliceT^.base := Sym.tU8; - results[0] := nil; - results[1] := Sym.tInt; - results[2] := Sym.tReal; - results[3] := sliceT; - results[4] := tuple2; - results[5] := tuple3; - results[6] := Sym.tF32; - results[7] := Sym.tI16; - results[8] := record3; -end; - -(* Variables *) - -(* the address of a variable of the procedure that has one: in the frame, - a chunk, or a slot holding its address *) -proc memAdr: Gen.Adr; -var a: Gen.Adr; c: INT; -begin - c := rnd(3); - if c = 0 then - a.base := Gen.bFrame; a.chunk := 0; a.hi := 0; a.off := slot(); if flag() then a.off := a.off + 8; end; - elsif c = 1 then - a.base := Gen.bGlobal; a.chunk := FirstGlob + rnd(NumGlobs); a.hi := 0; a.off := 8 * rnd(3); - else - a.base := Gen.bSlot; a.chunk := INT(slot()); a.hi := 0; a.off := 8 * rnd(3); - end; - return a; -end; - -(* an address that can go on the stack: frame or chunk *) -proc pushableAdr: Gen.Adr; -var a: Gen.Adr; -begin - a.chunk := 0; a.hi := 0; - if flag() then - a.base := Gen.bFrame; a.off := slot(); if flag() then a.off := a.off + 8; end; - else - a.base := Gen.bGlobal; a.chunk := FirstGlob + rnd(NumGlobs); a.off := 8 * rnd(3); - end; - return a; -end; - -proc regAdr(r: INT): Gen.Adr; -var a: Gen.Adr; -begin - a.base := Gen.bReg; a.chunk := r; a.hi := 0; a.off := 0; - return a; -end; - -proc stackAdr: Gen.Adr; -var a: Gen.Adr; -begin - a.base := Gen.bStack; a.chunk := 0; a.hi := 0; a.off := 8 * rnd(3); - return a; -end; - -proc pushIntVar; -var a: Gen.Adr; sz: INT; c: INT; -begin - sz := size(); - c := rnd(5); - if (c = 0) and (numRegs > 0) then - if flag() then Gen.setInPlace(regs[rnd(numRegs)]); end; - Gen.pushVar(regAdr(regs[rnd(numRegs)]), 8, false, Gen.kWord); - elsif c = 1 then - Gen.pushAdr(pushableAdr()); - a := stackAdr(); - Gen.pushVar(a, sz, flag(), Gen.kWord); - else - a := memAdr(); - Gen.pushVar(a, sz, flag(), Gen.kWord); - end; -end; - -proc intExpr(b: INT); forward; -proc realExpr(k: Gen.Kind; b: INT); forward; -proc callExpr(want, b: INT); forward; - -proc realKind: Gen.Kind; -begin - if flag() then return Gen.kF64; end; - return Gen.kF32; -end; - -proc intExpr(b: INT); -var c, sz, n: INT; sg, f: BOOL; k: Gen.Kind; sy: Scan.Sym; a: Gen.Adr; -begin - if b > 0 then c := rnd(14); else c := rnd(3); end; - match c of - | 0: Gen.pushConst(Gen.kWord, constVal()); - | 1: pushIntVar(); - | 2: pushIntVar(); - | 3, 4, 5: - intExpr(b - 1); intExpr(b - 1); - sy := Arith[rnd(10)]; sz := size(); sg := flag(); - Gen.op(sy, Gen.kWord, sz, sg); - | 6: - if flag() then - intExpr(b - 1); intExpr(b - 1); - sy := Cmps[rnd(6)]; sg := flag(); - Gen.compare(sy, Gen.kWord, sg); - else - k := realKind(); - realExpr(k, b - 1); realExpr(k, b - 1); - sy := Cmps[rnd(6)]; - Gen.compare(sy, k, false); - end; - | 7: - intExpr(b - 1); - n := rnd(4); sz := size(); sg := flag(); - if n = 0 then Gen.neg(Gen.kWord, sz, sg); - elsif n = 1 then Gen.complement(sz, sg); - elsif n = 2 then Gen.notBool(); - else Gen.narrow(sz, sg); end; - | 8: - (* an element of an array in the frame or a chunk *) - intExpr(b - 1); - a := pushableAdr(); - sz := size(); sg := flag(); - n := 10 + rnd(100); - Gen.checkIndex(n, pos()); - Gen.indexArray(a, sz); - a := stackAdr(); a.off := 0; - Gen.pushVar(a, sz, sg, Gen.kWord); - | 9: - (* an element of a slice, in a frame slot or on the stack, or in registers *) - n := rnd(3); - sz := size(); sg := flag(); - if n = 0 then - a := memAdr(); - if a.base = Gen.bGlobal then a.base := Gen.bFrame; end; - intExpr(b - 1); - f := flag(); - Gen.indexSlice(a, sz, f, pos()); - elsif n = 1 then - Gen.pushAdr(pushableAdr()); - intExpr(b - 1); - a := stackAdr(); a.off := 0; - f := flag(); - Gen.indexSlice(a, sz, f, pos()); - else - a.base := Gen.bReg; a.chunk := X.R13; a.hi := X.R14; a.off := 0; - intExpr(b - 1); - f := flag(); - Gen.indexSlice(a, sz, f, pos()); - end; - a := stackAdr(); a.off := 0; - Gen.pushVar(a, sz, sg, Gen.kWord); - | 10: - k := realKind(); - realExpr(k, b - 1); - Gen.realToInt(k); - | 11: callExpr(1, b - 1); - | 12: - (* x + 1, and the offset noted on a word *) - intExpr(b - 1); - Gen.pushConst(Gen.kWord, rnd(100) - 50); - Gen.op(Scan.sPlus, Gen.kWord, 8, false); - | else: - intExpr(b - 1); - Gen.pushConst(Gen.kWord, constVal()); - sy := Arith[rnd(10)]; sz := size(); sg := flag(); - Gen.op(sy, Gen.kWord, sz, sg); - end; -end; - -proc realExpr(k: Gen.Kind; b: INT); -var c, n: INT; sy: Scan.Sym; a: Gen.Adr; k2: Gen.Kind; v: I64; -begin - if b > 0 then c := rnd(9); else c := rnd(3); end; - match c of - | 0: - n := rnd(4); - if n = 0 then v := 3FF0000000000000H; - elsif n = 1 then v := 4000000000000000H; - elsif n = 2 then v := 0; - else v := 3FE0000000000000H; end; - Gen.pushConst(k, v); - | 1: - a := memAdr(); - if k = Gen.kF32 then Gen.pushVar(a, 4, false, k); else Gen.pushVar(a, 8, false, k); end; - | 2: - if numX > 0 then - if flag() then Gen.setInPlace(xregs[rnd(numX)]); end; - Gen.pushVar(regAdr(xregs[rnd(numX)]), 8, false, k); - else - Gen.pushAdr(pushableAdr()); - a := stackAdr(); - Gen.pushVar(a, 8, false, k); - end; - | 3, 4: - realExpr(k, b - 1); realExpr(k, b - 1); - sy := RealOps[rnd(4)]; - Gen.op(sy, k, 8, false); - | 5: - intExpr(b - 1); - Gen.intToReal(k, flag()); - | 6: - realExpr(k, b - 1); - Gen.neg(k, 8, false); - | 7: - if k = Gen.kF32 then k2 := Gen.kF64; else k2 := Gen.kF32; end; - realExpr(k2, b - 1); - Gen.realToReal(k2); - | else: callExpr(2, b - 1); - end; -end; - -(* A call of one of the procedures, with arguments of all kinds. want: 0 - for a statement, any result; 1 an integer is left on the stack, 2 a - real. *) -proc callExpr(want, b: INT); -var - c: Gen.CallInfo; sig: ^Sym.Type; i, n, kind, r, ni: INT; k: Gen.Kind; -begin - sig := Sym.newType(Sym.fProc, 8, 8); - sig^.variadic := rnd(5) = 0; - if want = 1 then - r := rnd(5); - if r = 0 then sig^.base := Sym.tBool; - elsif r = 1 then sig^.base := Sym.tI8; - elsif r = 2 then sig^.base := Sym.tU32; - elsif r = 3 then sig^.base := tuple2; - else sig^.base := Sym.tInt; end; - elsif want = 2 then - if flag() then sig^.base := Sym.tReal; else sig^.base := Sym.tF32; end; - else - sig^.base := results[rnd(9)]; - end; - c.sig := sig; c.param := nil; c.obj := nil; c.ext := rnd(4) = 0; - c.n := 0; c.target := 0; - c.chunk := FirstProc + rnd(NumProcs); - if rnd(5) = 0 then c.chunk := -1; c.ext := false; c.target := slot(); end; - n := rnd(8); - for i := 0 to n - 1 do - kind := rnd(4); - if kind = 0 then - k := realKind(); - realExpr(k, b); - c.isF[c.n] := true; c.pair[c.n] := false; - INC(c.n); - elsif kind = 1 then - intExpr(b); intExpr(b); - c.isF[c.n] := false; c.pair[c.n] := true; - c.isF[c.n + 1] := false; c.pair[c.n + 1] := false; - INC(c.n, 2); - else - intExpr(b); - c.isF[c.n] := false; c.pair[c.n] := false; - INC(c.n); - end; - end; - Gen.call(c); - (* what comes back: a value or two, or nothing, or a temporary *) - ni := 0; - if sig^.base # nil then - if sig^.base^.form = Sym.fSlice then ni := 0; - elsif sig^.base = tuple2 then ni := 2; - elsif Sym.behindPointer(sig^.base) then ni := 0; - else ni := 1; end; - end; - if want = 0 then - for i := 1 to ni do Gen.drop(); end; - elsif ni = 2 then - Gen.drop(); (* the second; the first is the value *) - end; -end; - -(* A condition, left as a BOOL on the stack *) -proc boolExpr(b: INT); -var sy: Scan.Sym; sg: BOOL; -begin - if flag() then - intExpr(b); intExpr(b); - sy := Cmps[rnd(6)]; sg := flag(); - Gen.compare(sy, Gen.kWord, sg); - else - intExpr(b); - end; -end; - -proc stmt(b: INT); forward; - -(* a variable assigned the value on top: its address, if on the stack, - having been pushed first *) -proc assign(b: INT); -var a: Gen.Adr; c, sz: INT; k: Gen.Kind; -begin - c := rnd(6); - sz := size(); - if c = 0 then - a := memAdr(); - intExpr(b); - Gen.popVar(a, sz); - elsif c = 1 then - Gen.pushAdr(pushableAdr()); - a := stackAdr(); - intExpr(b); - Gen.popVar(a, sz); - elsif (c = 2) and (numRegs > 0) then - a := regAdr(regs[rnd(numRegs)]); - Gen.setInPlace(a.chunk); - Gen.pushVar(a, 8, false, Gen.kWord); - intExpr(b); - Gen.op(Arith[rnd(3)], Gen.kWord, 8, false); - Gen.popVar(a, 8); - elsif (c = 3) and (numX > 0) then - k := realKind(); - a := regAdr(xregs[rnd(numX)]); - realExpr(k, b); - Gen.popVar(a, 8); - elsif c = 4 then - k := realKind(); - a := memAdr(); - realExpr(k, b); - if k = Gen.kF32 then Gen.popVar(a, 4); else Gen.popVar(a, 8); end; - else - a := memAdr(); - Gen.pushConst(Gen.kWord, constVal()); - Gen.popVar(a, sz); - end; -end; - -proc retStmt(b: INT); -var i: INT; -begin - if curRes = 1 then intExpr(b); - elsif curRes = 7 then intExpr(b); - elsif curRes = 2 then realExpr(Gen.kF64, b); - elsif curRes = 6 then realExpr(Gen.kF32, b); - elsif (curRes = 3) or (curRes = 4) then intExpr(b); intExpr(b); - elsif curRes = 5 then - for i := 0 to 2 do intExpr(b); Gen.resultTo(8 * i, 8); end; - elsif curRes = 8 then - Gen.pushAdr(pushableAdr()); - Gen.copyResult(0, 24); - end; - Gen.returnJump(); -end; - -proc stmt(b: INT); -var - c, n, lo, i, start, saved, sz, tmp: INT; L, E, S, fj, tj: INT; a, a2: Gen.Adr; sy: Scan.Sym; sg: BOOL; - mk: I64; f: BOOL; -begin - mk := Gen.tempMark(); - if b <= 0 then c := rnd(4); else c := rnd(22); end; - match c of - | 0, 1, 2, 3: assign(b); - | 4: - (* if cond then s end *) - L := Gen.NoLabel; - boolExpr(b); - Gen.jumpIf(L, false); - stmt(b - 1); - Gen.fix(L); - | 5: - (* while cond do s end *) - L := Gen.NoLabel; - Gen.alignLoop(); - start := Chunk.pc; - boolExpr(b); - Gen.jumpIf(L, false); - stmt(b - 1); - Gen.jumpBack(start); - Gen.fix(L); - | 6: - (* if a < b then s end, with a jump on the comparison *) - L := Gen.NoLabel; - intExpr(b); intExpr(b); - sy := Cmps[rnd(6)]; sg := flag(); - if flag() then Gen.jumpCmp(sy, sg, L); else Gen.jumpOut(sy, sg, L); end; - stmt(b - 1); - Gen.fix(L); - | 7: - (* v := a < b and c, a condition worked out into a BOOL *) - fj := Gen.NoLabel; tj := Gen.NoLabel; - intExpr(b); intExpr(b); - sy := Cmps[rnd(6)]; sg := flag(); - Gen.condValue(sy, sg, fj, tj); - a := memAdr(); - Gen.popVar(a, 1); - | 8: callExpr(0, b); - | 9: - (* a match through a jump table *) - a := memAdr(); - if a.base = Gen.bSlot then a.base := Gen.bFrame; a.off := slots[0]; end; - intExpr(b); - Gen.popVar(a, 8); - E := Gen.NoLabel; - n := 2 + rnd(4); lo := rnd(5) - 2; - if flag() then S := Gen.NoLabel; else S := Chunk.pc; end; - start := Gen.jumpTable(a.off, lo, n, S, E); - for i := 0 to n - 1 do Gen.tableEntry(start, Chunk.pc + 4 * (n - i)); end; - for i := 0 to n - 1 do - stmt(b - 1); - end; - Gen.fix(E); - | 10: - Gen.pushAdr(pushableAdr()); Gen.pushAdr(pushableAdr()); - n := 1 + rnd(150); lo := 8 * rnd(3); - Gen.copyBlock(n, lo); - | 11: - Gen.pushAdr(pushableAdr()); - a := memAdr(); - if flag() and (numRegs > 1) then - a.base := Gen.bReg; a.chunk := regs[0]; a.hi := regs[1]; a.off := 0; - end; - n := 1 + rnd(150); - Gen.copyTo(a, n); - | 12: - a := memAdr(); - n := 1 + rnd(100); - Gen.zeroVar(a, n); - | 13: - (* a slice of an array, of a slice, of a pointer *) - tmp := INT(Gen.allocTemp(16, 8)); - n := rnd(3); - sz := size(); sg := flag(); - if n = 0 then - a := pushableAdr(); - Gen.pushAdr(a); intExpr(b); intExpr(b); - a := stackAdr(); a.off := 0; - f := flag(); n := 10 + rnd(50); - Gen.slice(a, f, n, sz, tmp, sg, pos()); - elsif n = 1 then - a := memAdr(); - if a.base = Gen.bGlobal then a.base := Gen.bFrame; end; - intExpr(b); intExpr(b); - Gen.slice(a, false, 0, sz, tmp, sg, pos()); - else - intExpr(b); intExpr(b); intExpr(b); - Gen.slicePtr(sz, tmp, sg, pos()); - end; - | 14: - a.off := slot(); a2.off := slot(); sz := size(); f := flag(); - Gen.copySlice(a.off, a2.off, sz, f, pos()); - | 15: - n := rnd(4); sz := size(); sg := flag(); - if n = 0 then - Gen.pushAdr(pushableAdr()); intExpr(b); - f := flag(); - Gen.incDec(sz, sg, f); - elsif (n = 1) and (numRegs > 0) then - intExpr(b); - Gen.incDecReg(regs[0], sz, sg, flag()); - elsif n = 2 then - intExpr(b); - n := 3 + rnd(6); lo := FirstGlob + rnd(NumGlobs); - Gen.checkEnum(n, lo, pos()); - Gen.drop(); - else - intExpr(b); - n := rnd(8000); - Gen.checkNil(n, pos()); - Gen.drop(); - end; - | 16: - n := rnd(3); - if n = 0 then - a.off := slot(); a2.off := slot(); n := rnd(4); f := flag(); - Gen.compareSeq(a.off, a2.off, n, f); - Gen.drop(); - elsif n = 1 then - intExpr(b); - Gen.errorText(Gen.allocTemp(16, 8)); - else - intExpr(b); - n := 1 + rnd(200); - Gen.zeroNew(n); - Gen.drop(); - end; - | 17: - n := rnd(3); - if n = 0 then - intExpr(b); intExpr(b); intExpr(b); - Gen.atomic(Sym.bCas); - elsif n = 1 then - intExpr(b); intExpr(b); - Gen.atomic(Sym.bXchg); - else - intExpr(b); intExpr(b); - Gen.atomic(Sym.bXadd); - end; - Gen.drop(); - | 18: - n := rnd(7); - for i := 1 to n do intExpr(b); end; - Gen.sysCall(n); - Gen.drop(); - if flag() then Gen.fence(); end; - | 19: - (* defer s *) - L := Gen.NoLabel; - start, saved := Gen.deferBegin(L); - stmt(b - 1); - Gen.deferEnd(L, saved); - Gen.runDefer(start); - | 20: - if curRes # 0 then retStmt(b); else assign(b); end; - | else: - if curRes # 0 then - Gen.tryAlign(); - Gen.tryReturn(slot()); - else - assign(b); - end; - end; - Gen.releaseTemps(mk); -end; - -(* Each statement may be preceded by a few that are made and taken back, - as the parser does when it compiles a body a second time *) -proc genProc(c: INT); -var - sig: ^Sym.Type; params, p, last: ^Sym.Obj; i, n, kind, mask, j, ns, line: INT; t: ^Sym.Type; - mk: Gen.CodeMark; open: BOOL; -begin - sig := Sym.newType(Sym.fProc, 8, 8); - curRes := rnd(9); - sig^.base := results[curRes]; - params := nil; last := nil; - n := rnd(8); - for i := 0 to n - 1 do - kind := rnd(5); - if kind = 0 then t := Sym.tReal; - elsif kind = 1 then t := sliceT; - elsif kind = 2 then t := Sym.tI32; - else t := Sym.tInt; end; - p := Sym.newObj(Sym.oVar, 0, t); - p^.param := true; - p^.isVar := kind = 4; - if last = nil then params := p; else last^.next := p; end; - last := p; - end; - Gen.beginProc(sig, params); - for i := 0 to 5 do slots[i] := Gen.allocPerm(8, 8); end; - mask := rnd(32); - j := rnd(16); - mask := mask | (j shl Gen.XBit); - if rnd(3) = 0 then mask := 0; end; - numRegs := 0; numX := 0; - for j := 0 to 4 do - if (mask & (1 shl j)) # 0 then regs[numRegs] := Gen.VarRegs[j]; INC(numRegs); end; - end; - for j := 0 to 3 do - if (mask & (1 shl (Gen.XBit + j))) # 0 then - xregs[numX] := Gen.XVarRegs[j]; INC(numX); - Gen.xHome(Gen.XVarRegs[j], Gen.allocPerm(8, 8)); - end; - end; - (* some parameters live in registers: a register variable's, or kept - where they arrived *) - j := 0; - p := params; - while p # nil do - if (p^.typ = Sym.tInt) and (not p^.isVar) and (j < numRegs) and flag() then - p^.reg := regs[j]; INC(j); - elsif (p^.typ = Sym.tReal) and (numX > 0) and flag() then - p^.reg := xregs[0]; - elsif flag() then - Gen.keepArrived(p); - end; - p := p^.next; - end; - Gen.useRegs(mask); - line := 1 + rnd(40); - Gen.enterBody(c, line); - Gen.markCode(mk); - (* a body made and taken back *) - if flag() then - stmt(2); - Gen.rewind(mk); - Gen.redo(mk); (* the jumps to the exit went with the code *) - end; - ns := 2 + rnd(8); - for i := 1 to ns do stmt(3); end; - open := flag() and (sig^.base # nil); - if (sig^.base # nil) and (not open) then retStmt(2); end; - _ := Gen.endProc(c, open); - Fmt.print("proc {} lost {} regs {} saves {} at {}\n", [Fmt.int(c), Fmt.int(bit(Gen.lost)), Fmt.int(mask), - Fmt.int(Chunk.chunks[c].saveMask), Fmt.int(Chunk.chunks[c].saveAt)]); -end; - -(* The name p0, p1, ... of chunk i, interned *) -proc chunkNameOf(i: INT): INT; -var s: [3]U8; -begin - s[0] := 'p'; s[1] := U8(48 + i div 10); s[2] := U8(48 + i mod 10); - return Scan.intern(ADR(s[0]), 3); -end; - -proc main*; -var i, c: INT; path: []U8; links: [2]Gen.Link; entry: INT; dyn: BOOL; -begin - seed := 5; - path := "lib/Rt.raz"; - if LEN(Scan.open(path)) # 0 then Diag.fatal("no file"); end; - dyn := ARGC > 1; - X.setListing(true); - Chunk.setCurFile(Scan.intern(ADR(path[0]), LEN(path))); - for i := 0 to NumProcs - 1 do c := Chunk.newChunk(Chunk.secText, -1, chunkNameOf(i)); Chunk.setLive(c); end; - for i := 0 to NumGlobs - 1 do - c := Chunk.newChunk(Chunk.secBss, -1, chunkNameOf(NumProcs + i)); - Chunk.setBss(c, 64, 8); - end; - makeTypes(); - for i := 0 to NumProcs - 1 do genProc(FirstProc + i); end; - links[0].chunk := FirstProc; links[0].into := FirstGlob; - links[1].chunk := FirstProc + 1; links[1].into := FirstGlob + 1; - entry := Gen.entryPoint(dyn, nil, nil, FirstProc + 2, FirstProc + 3, links, 2); - Fmt.print("entry {} pc {} fixups {} lines {} said {}\n", [Fmt.int(entry), Fmt.int(Chunk.pc), - Fmt.int(Chunk.numFixups), Fmt.int(Chunk.numLines), Fmt.int(X.saidCount())]); - for i := 0 to Chunk.pc - 1 do Out.hex(U64(Chunk.text[i]), 2); end; - Out.ln; - for i := 0 to Chunk.numFixups - 1 do - Fmt.print("F {} {} {}\n", [Fmt.int(Chunk.fixups[i].at), Fmt.int(Chunk.fixups[i].chunk), Fmt.int(Chunk.fixups[i].addend)]); - end; - for i := 0 to Chunk.numLines - 1 do - Fmt.print("L {} {} {} {}\n", [Fmt.int(Chunk.lines[i].at), Fmt.int(Chunk.lines[i].chunk), - Fmt.int(Chunk.lines[i].line), Fmt.int(Chunk.lines[i].col)]); - end; - X.list(-1); -end; - -end GenDump. diff --git a/src/test/ItemDump.pas b/src/test/ItemDump.pas deleted file mode 100644 index 7a00c42..0000000 --- a/src/test/ItemDump.pas +++ /dev/null @@ -1,665 +0,0 @@ -{ Drives item with a fixed pseudo-random script of operands and - operations, with mistakes among them, and prints what each step gave - (the item, or that it failed, whose message goes to standard error), - then the bytes, fixups, line notes and listing (raz -S) of the code - it made: the seed's side of a test of src/Item.raz, whose - ItemDump.raz does and prints the same. The argument, a number, - changes the script. A run ends at the 20th error, as a compile does, - so several scripts are run. } -program itemdump; -{$mode objfpc}{$H-} -uses diag, scan, seedcontinue, sym, chunk, x64, gen, item; - -const - FirstProc = 3; - NumProcs = 30; - FirstGlob = FirstProc + NumProcs; - NumGlobs = 4; - Steps = 14; - YInt = 0; YI64 = 1; YI32 = 2; YU8 = 3; YU16 = 4; YU32 = 5; YU64 = 6; YBool = 7; - YReal = 8; YF32 = 9; YI8 = 10; YEnum = 11; YSet = 12; YPtr = 13; YVptr = 14; - YPtrArr = 15; YArr = 16; YArrU8 = 17; YSlice = 18; YBytes = 19; YRec = 20; - YPacked = 21; YVariant = 22; YProc0 = 23; YProc1 = 24; YProc2 = 25; - YPtrRec = 26; YErr = 27; YVrec = 28; YVarr = 29; - NumT = 30; - IntTypes: array[0..7] of longint = (YInt, YI64, YI32, YU8, YU16, YU32, YU64, YI8); - Ops: array[0..19] of TSym = (sPlus, sMinus, sTimes, sDiv, sMod, - sAmp, sBar, sXor, sShl, sShr, - sEql, sNeq, sLss, sLeq, sGtr, sGeq, - sSlash, sIn, sPlus, sMinus); - Cmps: array[0..5] of TSym = (sEql, sNeq, sLss, sLeq, sGtr, sGeq); - -var - Letters: string[9]; - seed: int64; - ty: array[0..NumT - 1] of PType; - vars: array[0..NumT - 1] of PObj; - procs: array[0..5] of PObj; - strs: array[0..2] of PObj; - arrConst, typeObj, builtinObj, caseObj: PObj; - mod0, mod1: PModule; - tuple2, record3, packedR, variantT: PType; - results: array[0..8] of PType; - curRes: longint; - call: TCall; - jb: jmp_buf; - gK: longint; - gX: TItem; - gOk: boolean; - -function Rnd(n: longint): longint; -begin - seed := (seed * 1103515245 + 12345) and $7FFFFFFF; - Rnd := (seed shr 8) mod n; -end; - -function Flag: boolean; -begin - Flag := Rnd(2) = 0; -end; - -function Bit(f: boolean): longint; -begin - if f then Bit := 1 else Bit := 0; -end; - -function Pos: TPos; -var p: TPos; -begin - p.f := 0; p.a := Rnd(400); p.b := p.a + 1 + Rnd(12); - Pos := p; -end; - -function ConstVal: int64; -var x: longint; r: int64; -begin - x := Rnd(1 shl 30); - case Rnd(7) of - 0: r := x mod 10; - 1: r := -(x mod 100); - 2: r := int64(1) shl (x mod 20); - 3: r := x; - 4: r := int64(x) * 4096 * 4096 * 9 - 7; - 5: r := -int64(x) * 8; - else r := x mod 1000 - 500; - end; - ConstVal := r; -end; - -function InternStr(const s: shortstring): longint; -begin - InternStr := Intern(@s[1], length(s)); -end; - -{ A name v00, v01, ... , interned } -function NameOf(i: longint): longint; -var s: string[3]; -begin - s := 'v' + chr(48 + i div 10) + chr(48 + i mod 10); - NameOf := Intern(@s[1], 3); -end; - -function NewObjs(cls: TClass; t: PType; n: longint; step: int64; at: longint): PObj; -var first, o, last: PObj; i: longint; -begin - first := nil; last := nil; - for i := 0 to n - 1 do begin - o := NewObj(cls, NameOf(at + i), t); - o^.val := step * i; - if last = nil then first := o else last^.next := o; - last := o; - end; - NewObjs := first; -end; - -function ProcType(res: PType; params: PObj; n: longint): PType; -var t: PType; -begin - t := NewType(fProc, 8, 8); - t^.base := res; - t^.fields := params; - t^.len := n; - ProcType := t; -end; - -function Param(t: PType; isVar: boolean; at: longint): PObj; -var o: PObj; -begin - o := NewObj(oVar, NameOf(at), t); - o^.param := true; - o^.isVar := isVar; - Param := o; -end; - -procedure MakeTypes; -var t: PType; p, q, r: PObj; i: longint; -begin - ty[YInt] := tInt; ty[YI64] := tI64; ty[YI32] := tI32; ty[YU8] := tU8; - ty[YU16] := tU16; ty[YU32] := tU32; ty[YU64] := tU64; ty[YBool] := tBool; - ty[YReal] := tReal; ty[YF32] := tF32; ty[YI8] := tI8; ty[YErr] := tError; - ty[YBytes] := tBytes; - t := NewType(fEnum, 1, 1); t^.len := 5; ty[YEnum] := t; - t := NewType(fSet, 8, 8); t^.len := 5; t^.base := ty[YEnum]; ty[YSet] := t; - t := NewType(fPtr, 8, 8); t^.base := tI64; ty[YPtr] := t; - t := NewType(fPtr, 8, 8); t^.base := tI64; t^.vol := true; ty[YVptr] := t; - t := NewType(fArray, 48, 8); t^.base := tI64; t^.len := 6; ty[YArr] := t; - t := NewType(fPtr, 8, 8); t^.base := ty[YArr]; ty[YPtrArr] := t; - t := NewType(fPtr, 8, 8); t^.base := ty[YArr]; t^.vol := true; ty[YVarr] := t; - t := NewType(fArray, 8, 1); t^.base := tU8; t^.len := 8; ty[YArrU8] := t; - t := NewType(fSlice, 16, 8); t^.base := tI64; ty[YSlice] := t; - record3 := NewType(fRecord, 24, 8); - p := NewObj(oField, NameOf(40), tI64); p^.val := 0; - q := NewObj(oField, NameOf(41), tReal); q^.val := 8; p^.next := q; - r := NewObj(oField, NameOf(42), tI32); r^.val := 16; q^.next := r; - record3^.fields := p; - ty[YRec] := record3; - t := NewType(fPtr, 8, 8); t^.base := record3; ty[YPtrRec] := t; - t := NewType(fPtr, 8, 8); t^.base := record3; t^.vol := true; ty[YVrec] := t; - packedR := NewType(fRecord, 53, 1); - packedR^.packd := true; - p := NewObj(oField, NameOf(43), tU8); p^.val := 0; - q := NewObj(oField, NameOf(44), tI32); q^.val := 1; p^.next := q; - r := NewObj(oField, NameOf(46), ty[YArr]); r^.val := 5; q^.next := r; - packedR^.fields := p; - ty[YPacked] := packedR; - variantT := NewType(fVariant, 16, 8); - ty[YVariant] := variantT; - tuple2 := NewType(fRecord, 16, 8); - tuple2^.tuple := true; tuple2^.len := 2; tuple2^.fields := NewObjs(oVar, tInt, 2, 8, 45); - p := Param(tInt, false, 46); p^.next := Param(tReal, false, 47); - ty[YProc0] := ProcType(tInt, p, 2); - ty[YProc1] := ProcType(nil, nil, 0); - p := Param(tI64, true, 48); p^.next := Param(ty[YSlice], false, 49); - ty[YProc2] := ProcType(tuple2, p, 2); - results[0] := nil; results[1] := tInt; results[2] := tReal; results[3] := ty[YSlice]; - results[4] := tuple2; results[5] := record3; results[6] := tF32; results[7] := tI16; - results[8] := ty[YBytes]; - for i := 0 to NumT - 1 do - if ty[i] = nil then Fatal('a type is missing'); -end; - -procedure MakeObjs; -var i, c: longint; o: PObj; -begin - New(mod0); mod0^.name := InternStr('Zero'); - New(mod1); mod1^.name := InternStr('Other'); - curModule := mod0; - for i := 0 to 5 do begin - o := NewObj(oProc, NameOf(50 + i), ty[YProc0 + i mod 3]); - o^.chunk := FirstProc + Rnd(NumProcs); - o^.ext := i >= 4; - o^.isCode := i = 3; - procs[i] := o; - end; - for i := 0 to 2 do begin - o := NewObj(oConst, NameOf(60 + i), tStr); - o^.val := 2 + 3 * i; - c := NewChunk(secRodata, -1, -1); - SetData(c, @Letters[1], o^.val + 1, 1); - o^.chunk := c; - strs[i] := o; - end; - arrConst := NewObj(oConst, NameOf(63), ty[YArr]); - arrConst^.chunk := NewChunk(secRodata, -1, -1); - SetData(arrConst^.chunk, @Letters[1], 8, 8); - typeObj := NewObj(oType, NameOf(64), ty[YRec]); - builtinObj := NewObj(oBuiltin, NameOf(65), tNone); - caseObj := NewObj(oCase, NameOf(66), variantT); - caseObj^.val := 3; - caseObj^.fields := NewObjs(oField, tInt, 2, 8, 67); - caseObj^.fields^.val := 8; -end; - -procedure MakeVars; -var i, k: longint; o: PObj; t: PType; -begin - for i := 0 to NumT - 1 do begin - t := ty[i]; - o := NewObj(oVar, NameOf(i), t); - o^.lev := 1; - k := Rnd(9); - if k = 2 then begin - o^.lev := 0; o^.chunk := FirstGlob + Rnd(NumGlobs); - end else if k = 3 then begin - o^.ind := true; o^.val := AllocPerm(8, 8); o^.off := 8 * Rnd(3); - end else if k = 4 then begin - if (t^.form in [fInt, fPtr]) and (t^.size = 8) then - o^.reg := VarRegs[Rnd(5)] - else if t^.form = fReal then - o^.reg := XVarRegs[Rnd(4)] - else if t^.form = fSlice then begin - o^.reg := VarRegs[Rnd(3)]; o^.reg2 := VarRegs[3 + Rnd(2)]; - end; - if o^.reg = 0 then o^.val := AllocPerm(t^.size, 8); - end else if k = 5 then begin - o^.lev := 0; o^.chunk := FirstGlob + Rnd(NumGlobs); o^.module := mod1; - end else if k = 6 then begin - o^.lev := 0; o^.chunk := FirstGlob + Rnd(NumGlobs); o^.ext := true; - end else - o^.val := AllocPerm(t^.size + 8, 8); - vars[i] := o; - end; -end; - -{ Operands } - -function IntType: PType; -begin - IntType := ty[IntTypes[Rnd(8)]]; -end; - -procedure VarItem(var x: TItem; t: PType); -var i, k: longint; -begin - k := Rnd(NumT); - for i := 0 to NumT - 1 do - if ty[i] = t then k := i; - FillChar(x, SizeOf(x), 0); - x.pos := Pos; - MakeItem(x, vars[k]); -end; - -procedure Operand(var x: TItem; t: PType); -var k: longint; r: double; v: int64; -begin - FillChar(x, SizeOf(x), 0); - x.pos := Pos; - if t = nil then begin - case Rnd(14) of - 0: MakeConst(x, tConst, ConstVal); - 1: MakeConst(x, tConst, ConstVal mod 200); - 2: begin t := IntType; v := ConstVal mod 100; MakeConst(x, t, v) end; - 3: begin r := (ConstVal mod 1000) / 8.0; MakeReal(x, r) end; - 4: MakeConst(x, tBool, Rnd(2)); - 5: MakeItem(x, strs[Rnd(3)]); - 6: MakeConst(x, tNil, 0); - 7: MakeItem(x, procs[Rnd(6)]); - 8: begin - if Rnd(4) = 0 then begin - case Rnd(3) of - 0: MakeItem(x, typeObj); - 1: MakeItem(x, builtinObj); - else MakeItem(x, caseObj); - end; - end else - MakeItem(x, arrConst); - end; - 13: begin MakeItem(x, vars[Rnd(NumT)]); Value(x) end; - else VarItem(x, ty[Rnd(NumT)]); - end; - exit; - end; - k := Rnd(10); - if IsInt(t) then begin - if k < 3 then begin MakeConst(x, tConst, ConstVal mod 100); exit end; - if k = 3 then begin MakeConst(x, t, ConstVal mod 100); exit end; - end else if t^.form = fReal then begin - if k < 3 then begin r := (ConstVal mod 1000) / 8.0; MakeReal(x, r); exit end; - end else if t^.form = fBool then begin - if k < 3 then begin MakeConst(x, tBool, Rnd(2)); exit end; - end else if t^.form = fPtr then begin - if k < 2 then begin MakeConst(x, tNil, 0); exit end; - end else if (t^.form = fSlice) and (t^.base = tU8) then begin - if k < 3 then begin MakeItem(x, strs[Rnd(3)]); exit end; - end; - VarItem(x, t); -end; - -procedure CondItem(var x: TItem); -var y: TItem; L: longint; op: TSym; t: PType; -begin - t := IntType; - op := Cmps[Rnd(6)]; - Operand(x, t); - Op1(op, x, L); - Operand(y, t); - Op2(op, x, y, L); -end; - -procedure Place(var x: TItem); -var i, k: longint; f: PObj; y, lo, hi: TItem; -begin - VarItem(x, ty[Rnd(NumT)]); - for i := 1 to 3 do begin - if Rnd(3) <> 0 then begin - k := Rnd(6); - if (k = 0) or (x.typ^.form = fPtr) then - Deref(x) - else if x.typ^.form = fRecord then begin - f := x.typ^.fields; - k := Rnd(3); - while (k > 0) and (f^.next <> nil) do begin f := f^.next; dec(k) end; - Field(x, f); - end else if x.typ^.form in [fArray, fSlice, fStr] then begin - IndexPrep(x); - if k < 4 then begin - Operand(y, IntType); - Index(x, y); - end else begin - Operand(lo, IntType); - SliceLo(lo); - Operand(hi, IntType); - Slice(x, lo, hi); - end; - end; - end; - end; -end; - -procedure Dump(var x: TItem); -var name: shortstring; flags: longint; -begin - name := '-'; - if x.typ <> nil then name := TypeName(x.typ); - flags := Bit(x.vol) + 2 * Bit(x.isVar) + 4 * Bit(x.odd) + 8 * Bit(x.ro) + 16 * Bit(x.deref); - write('I ', ord(x.mode), ' ', name, ' ', ord(x.rel), ' ', Bit(x.relSigned), ' ', x.fj, ' ', x.tj, ' ', x.val); - writeln(' ', ord(x.base), ' ', x.chunk, ' ', x.off, ' ', flags, ' depth ', depth, ' pushed ', pushed); -end; - -procedure DoCall(var x: TItem); -var f, a: TItem; p: PObj; n: longint; -begin - FillChar(f, SizeOf(f), 0); - f.pos := Pos; - if Rnd(5) = 0 then MakeItem(f, vars[YProc0 + Rnd(3)]) - else MakeItem(f, procs[Rnd(6)]); - BeginCall(call, f); - p := call.param; - n := Rnd(4) - 1; - while (p <> nil) or (n > 0) do begin - if p <> nil then begin - if p^.isVar and (Rnd(2) = 0) then begin - VarItem(a, ty[YVptr]); - Deref(a); - end else if Rnd(6) = 0 then Operand(a, nil) - else Operand(a, p^.typ); - p := p^.next; - end else begin - Operand(a, nil); - n := n - 1; - end; - PassArg(call, a); - if (p = nil) and (n < 0) then n := 0; - if (p <> nil) and (n < 0) and (Rnd(4) = 0) then begin p := nil; n := 0 end; - end; - EndCall(call, x); -end; - -procedure Binary(var x: TItem); -var y: TItem; L, M: longint; op: TSym; t: PType; -begin - L := NoLabel; - M := NoLabel; - op := Ops[Rnd(20)]; - case Rnd(8) of - 0, 1: t := IntType; - 2: t := ty[YReal + Rnd(2)]; - 3: t := ty[YBool]; - 4: t := ty[YEnum + Rnd(2)]; - else t := ty[Rnd(NumT)]; - end; - if Rnd(6) = 0 then begin - if Flag then op := sAnd else op := sOr; - CondItem(x); - Op1(op, x, L); - CondItem(y); - end else if op = sIn then begin - Operand(x, ty[YEnum]); - Op1(op, x, L); - Operand(y, ty[YSet]); - end else begin - Operand(x, t); - Op1(op, x, L); - if Rnd(5) = 0 then Operand(y, nil) else Operand(y, t); - end; - Op2(op, x, y, L); - if (x.mode = mCond) and (Rnd(3) <> 0) then begin - if Flag then CondJump(x, M) else CondJumpTrue(x, M); - Fix(M); - x.mode := mNone; - end; -end; - -procedure Unary(var x: TItem); -begin - Operand(x, nil); - case Rnd(3) of - 0: Neg(x); - 1: NotOp(x); - else Complement(x); - end; -end; - -procedure DoMatch(var x: TItem); -var p, lo, hi: int64; L: longint; -begin - L := NoLabel; - case Rnd(3) of - 0: VarItem(x, ty[YVariant]); - 1: VarItem(x, ty[YEnum]); - else Operand(x, nil); - end; - p := MatchBegin(x); - MatchTag(p, ConstVal mod 10, L); - lo := ConstVal mod 10; - hi := ConstVal mod 10 + 10; - MatchRange(p, lo, hi, L); - MatchRange(p, 3, 3, L); - Fix(L); -end; - -procedure DoFor(var x: TItem); -var o: PObj; lim: TItem; step: int64; exitL: longint; -begin - o := vars[IntTypes[Rnd(8)]]; - step := ConstVal mod 5; - if step = 0 then step := 1; - if Rnd(2) = 0 then step := -step; - FillChar(lim, SizeOf(lim), 0); - lim.pos := Pos; - MakeConst(lim, o^.typ, ConstVal mod 50); - exitL := NoLabel; - ForStep(o, step); - ForNext(o, lim, step, exitL, pc, pc); - Fix(exitL); - x := lim; -end; - -procedure DoMisc(var x: TItem); -var y: TItem; -begin - case Rnd(5) of - 0: begin - Place(x); - Address(x); - Operand(y, x.typ); - IncDec(x, y, Flag); - end; - 1: begin - Place(x); - Pin(x); - end; - 2: begin - Place(x); - CopyDest(x); - Place(y); - CopyFrom(x, y); - end; - 3: begin - Operand(x, nil); - MakeStr(y); - Coerce(y, ty[YBytes]); - end; - else begin - Operand(x, nil); - NilTest(x); - UsesReals(x.pos); - Discard(x); - end; - end; -end; - -procedure DoTemp(var x: TItem); -var a: TItem; c, h, n: longint; f: PObj; -begin - FillChar(x, SizeOf(x), 0); - x.pos := Pos; - case Rnd(5) of - 0: ZeroTemp(x, ty[YVariant]); - 1: begin - Construct(x, caseObj); - f := caseObj^.fields; - Operand(a, f^.typ); - SetField(x, f, a); - end; - 2: begin - ZeroTemp(x, ty[YRec]); - f := ty[YRec]^.fields; - if Flag then f := f^.next; - Operand(a, f^.typ); - SetField(x, f, a); - end; - 3: begin - Operand(a, nil); - h := Rnd(3) - 1; - n := Rnd(3) - 1; - c := ErrorChunk(a, h, n); - MakeConst(x, tError, int64(c) + 1); - end; - else begin - Operand(x, ty[YErr]); - Msg(x); - end; - end; -end; - -procedure DoReturn(var x: TItem); -var f: PObj; -begin - if (curRes = 4) and Flag then begin - f := tuple2^.fields; - if Flag then f := f^.next; - if Flag then begin - Operand(x, f^.typ); - ReturnField(f, x); - exit; - end; - DoCall(x); - ReturnAll(x); - exit; - end; - if Rnd(4) = 0 then begin ReturnNone; exit end; - Operand(x, results[curRes]); - ReturnValue(x); -end; - -procedure Step(k: longint; var x: TItem); -var y: TItem; -begin - FillChar(x, SizeOf(x), 0); - case k of - 0: begin Operand(x, nil); Value(x) end; - 1: begin Operand(x, nil); Address(x) end; - 2: begin Operand(x, nil); Coerce(x, ty[Rnd(NumT)]) end; - 3: begin Operand(x, nil); CoerceExact(x, IntType, '') end; - 4: begin - Place(x); - Operand(y, x.typ); - Assign(x, y); - end; - 5: Unary(x); - 6: Binary(x); - 7: begin Operand(x, nil); Convert(x, ty[Rnd(NumT)]) end; - 8: Place(x); - 9: DoCall(x); - 10: DoMatch(x); - 11: DoFor(x); - 12: DoMisc(x); - 13: DoTemp(x); - 14: DoReturn(x); - else Binary(x); - end; -end; - -{ A step, and what it leaves, each a recovery point as the parser's - statements are } -procedure RunStep; -begin - gOk := false; - recovery := @jb; - if setjmp(jb) = 0 then begin - Step(gK, gX); - gOk := true; - end; - recovery := nil; -end; - -procedure RunDiscard; -begin - recovery := @jb; - if setjmp(jb) = 0 then Discard(gX); - recovery := nil; -end; - -procedure GenProc(c: longint); -var sig: PType; i, line: longint; -begin - curRes := Rnd(9); - sig := ProcType(results[curRes], nil, 0); - BeginProc(sig, nil); - for i := 0 to 5 do AllocPerm(8, 8); - MakeVars; - checks := Flag; - line := 1 + Rnd(40); - EnterBody(c, line); - for i := 1 to Steps do begin - if errors >= 19 then break; - gK := Rnd(17); - demoted := false; - RunStep; - if not gOk then - writeln('S ', c, ' ', gK, ' failed') - else begin - writeln('S ', c, ' ', gK, ' ok'); - Dump(gX); - if (gK < 4) or ((gK >= 5) and (gK <= 9)) then RunDiscard; - end; - writeln('demoted ', Bit(demoted)); - ResetStack(0, 0); - end; - EndProc(c, sig^.base <> nil); -end; - -function ChunkNameOf(i: longint): longint; -var s: string; -begin - s := 'p' + chr(48 + i div 10) + chr(48 + i mod 10); - ChunkNameOf := Intern(@s[1], 3); -end; - -var - i, c: longint; path: string; -begin - seed := 5; - for i := 1 to length(ParamStr(1)) do seed := seed * 10 + ord(ParamStr(1)[i]) - 48; - Letters := 'abcdefgh' + #0; - path := 'src/test/ItemDump.txt'; - if Open(path) <> '' then Fatal('no file'); - Next; - listing := true; - curFile := Intern(@path[1], length(path)); - for i := 0 to NumProcs - 1 do begin c := NewChunk(secText, -1, ChunkNameOf(i)); chunks^[c].live := true end; - for i := 0 to NumGlobs - 1 do begin - c := NewChunk(secBss, -1, ChunkNameOf(NumProcs + i)); - SetBss(c, 64, 8); - end; - MakeTypes; - MakeObjs; - for i := 0 to NumProcs - 1 do GenProc(FirstProc + i); - writeln('pc ', pc, ' fixups ', numFixups, ' lines ', numLines, ' said ', SaidCount); - for i := 0 to pc - 1 do write(HexStr(code^[i], 2)); - writeln; - for i := 0 to numFixups - 1 do writeln('F ', fixups^[i].at, ' ', fixups^[i].chunk, ' ', fixups^[i].addend); - for i := 0 to numLines - 1 do writeln('L ', lines^[i].at, ' ', lines^[i].chunk, ' ', lines^[i].line, ' ', lines^[i].col); - List(-1); -end. diff --git a/src/test/ItemDump.raz b/src/test/ItemDump.raz deleted file mode 100644 index 8e4b126..0000000 --- a/src/test/ItemDump.raz +++ /dev/null @@ -1,679 +0,0 @@ -(* Drives Item with a fixed pseudo-random script of operands and - operations, with mistakes among them, and prints what each step gave - (the item, or that it failed, whose message goes to standard error), - then the bytes, fixups, line notes and listing (raz -S) of the code - it made: src/Item.raz's side of a test against the seed's, - ItemDump.pas, which does and prints the same. The argument, a digit, - changes the script. A run ends at the 20th error, as a compile does, - so several scripts are run. *) -module ItemDump; -import Out, Fmt, Files, Heap, Diag, Scan, Sym, Chunk, X := X64, Gen, Item; - -const - FirstProc = 3; (* chunks 0 to 2 are Gen's own *) - NumProcs = 30; - FirstGlob = FirstProc + NumProcs; - NumGlobs = 4; - Steps = 14; - (* the types of the script, by number *) - YInt = 0; YI64 = 1; YI32 = 2; YU8 = 3; YU16 = 4; YU32 = 5; YU64 = 6; YBool = 7; - YReal = 8; YF32 = 9; YI8 = 10; YEnum = 11; YSet = 12; YPtr = 13; YVptr = 14; - YPtrArr = 15; YArr = 16; YArrU8 = 17; YSlice = 18; YBytes = 19; YRec = 20; - YPacked = 21; YVariant = 22; YProc0 = 23; YProc1 = 24; YProc2 = 25; - YPtrRec = 26; YErr = 27; YVrec = 28; YVarr = 29; - NumT = 30; - IntTypes: [8]INT = [YInt, YI64, YI32, YU8, YU16, YU32, YU64, YI8]; - Ops: [20]Scan.Sym = [Scan.sPlus, Scan.sMinus, Scan.sTimes, Scan.sDiv, Scan.sMod, - Scan.sAmp, Scan.sBar, Scan.sXor, Scan.sShl, Scan.sShr, - Scan.sEql, Scan.sNeq, Scan.sLss, Scan.sLeq, Scan.sGtr, Scan.sGeq, - Scan.sSlash, Scan.sIn, Scan.sPlus, Scan.sMinus]; - Cmps: [6]Scan.Sym = [Scan.sEql, Scan.sNeq, Scan.sLss, Scan.sLeq, Scan.sGtr, Scan.sGeq]; - -var - Letters: []U8; - seed: I64; - ty: [NumT]^Sym.Type; - vars: [NumT]^Sym.Obj; (* a variable of each type, placed anew for each procedure *) - procs: [6]^Sym.Obj; - strs: [3]^Sym.Obj; - arrConst, typeObj, builtinObj, caseObj: ^Sym.Obj; - mod0, mod1: ^Sym.Module; - tuple2, record3, packedR, variantT: ^Sym.Type; - results: [9]^Sym.Type; - curRes: INT; (* the result of the procedure, an index of results *) - call: Gen.CallInfo; - -proc rnd(n: INT): INT; -begin - seed := (seed * 1103515245 + 12345) & 7FFFFFFFH; - return INT((seed shr 8) mod I64(n)); -end; - -proc flag: BOOL; -begin - return rnd(2) = 0; -end; - -proc bit(f: BOOL): INT; -begin - if f then return 1; end; - return 0; -end; - -proc pos: Diag.Pos; -var p: Diag.Pos; -begin - p.f := 0; p.a := rnd(400); p.b := p.a + 1 + rnd(12); - return p; -end; - -proc constVal: I64; -var x: INT; r: I64; -begin - x := rnd(1 shl 30); - match rnd(7) of - | 0: r := x mod 10; - | 1: r := -(x mod 100); - | 2: r := I64(1) shl (x mod 20); - | 3: r := x; - | 4: r := I64(x) * 4096 * 4096 * 9 - 7; - | 5: r := -I64(x) * 8; - | else: r := x mod 1000 - 500; - end; - return r; -end; - -(* A name, interned *) -proc intern(s: []U8): INT; -begin - return Scan.intern(ADR(s[0]), LEN(s)); -end; - -(* A name v00, v01, ... , interned *) -proc nameOf(i: INT): INT; -var s: [3]U8; -begin - s[0] := 'v'; s[1] := U8(48 + i div 10); s[2] := U8(48 + i mod 10); - return Scan.intern(ADR(s[0]), 3); -end; - -proc newObjs(cls: Sym.Class; t: ^Sym.Type; n: INT; step: I64; at: INT): ^Sym.Obj; -var first, o, last: ^Sym.Obj; i: INT; -begin - first := nil; last := nil; - for i := 0 to n - 1 do - o := Sym.newObj(cls, nameOf(at + i), t); - o^.val := step * I64(i); - if last = nil then first := o; else last^.next := o; end; - last := o; - end; - return first; -end; - -proc newType(form: Sym.Form; size, align: I64): ^Sym.Type; -begin - return Sym.newType(form, size, align); -end; - -proc procType(res: ^Sym.Type; params: ^Sym.Obj; n: INT): ^Sym.Type; -var t: ^Sym.Type; -begin - t := newType(Sym.fProc, 8, 8); - t^.base := res; - t^.fields := params; - t^.len := n; - return t; -end; - -proc param(t: ^Sym.Type; isVar: BOOL; at: INT): ^Sym.Obj; -var o: ^Sym.Obj; -begin - o := Sym.newObj(Sym.oVar, nameOf(at), t); - o^.param := true; - o^.isVar := isVar; - return o; -end; - -proc makeTypes; -var t: ^Sym.Type; p, q, r: ^Sym.Obj; i: INT; -begin - ty[YInt] := Sym.tInt; ty[YI64] := Sym.tI64; ty[YI32] := Sym.tI32; ty[YU8] := Sym.tU8; - ty[YU16] := Sym.tU16; ty[YU32] := Sym.tU32; ty[YU64] := Sym.tU64; ty[YBool] := Sym.tBool; - ty[YReal] := Sym.tReal; ty[YF32] := Sym.tF32; ty[YI8] := Sym.tI8; ty[YErr] := Sym.tError; - ty[YBytes] := Sym.tBytes; - t := newType(Sym.fEnum, 1, 1); t^.len := 5; ty[YEnum] := t; - t := newType(Sym.fSet, 8, 8); t^.len := 5; t^.base := ty[YEnum]; ty[YSet] := t; - t := newType(Sym.fPtr, 8, 8); t^.base := Sym.tI64; ty[YPtr] := t; - t := newType(Sym.fPtr, 8, 8); t^.base := Sym.tI64; t^.vol := true; ty[YVptr] := t; - t := newType(Sym.fArray, 48, 8); t^.base := Sym.tI64; t^.len := 6; ty[YArr] := t; - t := newType(Sym.fPtr, 8, 8); t^.base := ty[YArr]; ty[YPtrArr] := t; - t := newType(Sym.fPtr, 8, 8); t^.base := ty[YArr]; t^.vol := true; ty[YVarr] := t; - t := newType(Sym.fArray, 8, 1); t^.base := Sym.tU8; t^.len := 8; ty[YArrU8] := t; - t := newType(Sym.fSlice, 16, 8); t^.base := Sym.tI64; ty[YSlice] := t; - (* a record of an I64, an F64 and an I32 *) - record3 := newType(Sym.fRecord, 24, 8); - p := Sym.newObj(Sym.oField, nameOf(40), Sym.tI64); p^.val := 0; - q := Sym.newObj(Sym.oField, nameOf(41), Sym.tReal); q^.val := 8; p^.next := q; - r := Sym.newObj(Sym.oField, nameOf(42), Sym.tI32); r^.val := 16; q^.next := r; - record3^.fields := p; - ty[YRec] := record3; - t := newType(Sym.fPtr, 8, 8); t^.base := record3; ty[YPtrRec] := t; - t := newType(Sym.fPtr, 8, 8); t^.base := record3; t^.vol := true; ty[YVrec] := t; - (* a packed record: a byte, then an I32 that may be misaligned *) - packedR := newType(Sym.fRecord, 53, 1); - packedR^.packd := true; - p := Sym.newObj(Sym.oField, nameOf(43), Sym.tU8); p^.val := 0; - q := Sym.newObj(Sym.oField, nameOf(44), Sym.tI32); q^.val := 1; p^.next := q; - r := Sym.newObj(Sym.oField, nameOf(46), ty[YArr]); r^.val := 5; q^.next := r; - packedR^.fields := p; - ty[YPacked] := packedR; - variantT := newType(Sym.fVariant, 16, 8); - ty[YVariant] := variantT; - tuple2 := newType(Sym.fRecord, 16, 8); - tuple2^.tuple := true; tuple2^.len := 2; tuple2^.fields := newObjs(Sym.oVar, Sym.tInt, 2, 8, 45); - (* procedures: (I64, F64) I64; (); (var I64, []I64) of two results *) - p := param(Sym.tInt, false, 46); p^.next := param(Sym.tReal, false, 47); - ty[YProc0] := procType(Sym.tInt, p, 2); - ty[YProc1] := procType(nil, nil, 0); - p := param(Sym.tI64, true, 48); p^.next := param(ty[YSlice], false, 49); - ty[YProc2] := procType(tuple2, p, 2); - results[0] := nil; results[1] := Sym.tInt; results[2] := Sym.tReal; results[3] := ty[YSlice]; - results[4] := tuple2; results[5] := record3; results[6] := Sym.tF32; results[7] := Sym.tI16; - results[8] := ty[YBytes]; - for i := 0 to NumT - 1 do - if ty[i] = nil then Diag.fatal("a type is missing"); end; - end; -end; - -(* The objects named by the script: procedures, strings, constants of - an array, a type, a built-in, a case of a variant *) -proc makeObjs; -var i, c: INT; o: ^Sym.Obj; -begin - NEW(mod0, Heap.heap); mod0^.name := intern("Zero"); - NEW(mod1, Heap.heap); mod1^.name := intern("Other"); - Sym.setCurModule(mod0); - for i := 0 to 5 do - o := Sym.newObj(Sym.oProc, nameOf(50 + i), ty[YProc0 + i mod 3]); - o^.chunk := FirstProc + rnd(NumProcs); - o^.ext := i >= 4; - o^.isCode := i = 3; - procs[i] := o; - end; - for i := 0 to 2 do - o := Sym.newObj(Sym.oConst, nameOf(60 + i), Sym.tStr); - o^.val := 2 + 3 * i; - c := Chunk.newChunk(Chunk.secRodata, -1, -1); - Chunk.setData(c, ADR(Letters[0]), INT(o^.val) + 1, 1); - o^.chunk := c; - strs[i] := o; - end; - arrConst := Sym.newObj(Sym.oConst, nameOf(63), ty[YArr]); - arrConst^.chunk := Chunk.newChunk(Chunk.secRodata, -1, -1); - Chunk.setData(arrConst^.chunk, ADR(Letters[0]), 8, 8); - typeObj := Sym.newObj(Sym.oType, nameOf(64), ty[YRec]); - builtinObj := Sym.newObj(Sym.oBuiltin, nameOf(65), Sym.tNone); - caseObj := Sym.newObj(Sym.oCase, nameOf(66), variantT); - caseObj^.val := 3; - caseObj^.fields := newObjs(Sym.oField, Sym.tInt, 2, 8, 67); - caseObj^.fields^.val := 8; -end; - -(* The variable of each type, in the frame, a chunk, a slot, a register, - another module or C's, as the procedure compiled has them *) -proc makeVars; -var i, k: INT; o: ^Sym.Obj; t: ^Sym.Type; -begin - for i := 0 to NumT - 1 do - t := ty[i]; - o := Sym.newObj(Sym.oVar, nameOf(i), t); - o^.lev := 1; - k := rnd(9); - if k = 2 then - o^.lev := 0; o^.chunk := FirstGlob + rnd(NumGlobs); - elsif k = 3 then - o^.ind := true; o^.val := Gen.allocPerm(8, 8); o^.off := 8 * rnd(3); - elsif k = 4 then - if (t^.form in {Sym.fInt, Sym.fPtr}) and (t^.size = 8) then - o^.reg := Gen.VarRegs[rnd(5)]; - elsif t^.form = Sym.fReal then - o^.reg := Gen.XVarRegs[rnd(4)]; - elsif t^.form = Sym.fSlice then - o^.reg := Gen.VarRegs[rnd(3)]; o^.reg2 := Gen.VarRegs[3 + rnd(2)]; - end; - if o^.reg = 0 then o^.val := Gen.allocPerm(t^.size, 8); end; - elsif k = 5 then - o^.lev := 0; o^.chunk := FirstGlob + rnd(NumGlobs); o^.home := mod1; - elsif k = 6 then - o^.lev := 0; o^.chunk := FirstGlob + rnd(NumGlobs); o^.ext := true; - else - o^.val := Gen.allocPerm(t^.size + 8, 8); - end; - vars[i] := o; - end; -end; - -(* Operands *) - -proc intType: ^Sym.Type; -begin - return ty[IntTypes[rnd(8)]]; -end; - -proc varItem(var x: Item.Item; t: ^Sym.Type): ERROR; -var i, k: INT; -begin - k := rnd(NumT); - for i := 0 to NumT - 1 do - if ty[i] = t then k := i; end; - end; - x := {}; - x.pos := pos(); - return Item.makeItem(x, vars[k]); -end; - -(* An operand of type t, or of any if t is nil *) -proc operand(var x: Item.Item; t: ^Sym.Type): ERROR; -var k: INT; r: F64; v: I64; -begin - x := {}; - x.pos := pos(); - if t = nil then - match rnd(14) of - | 0: Item.makeConst(x, Sym.tConst, constVal()); - | 1: Item.makeConst(x, Sym.tConst, constVal() mod 200); - | 2: t := intType(); v := constVal() mod 100; Item.makeConst(x, t, v); - | 3: r := F64(constVal() mod 1000) / 8.0; Item.makeReal(x, r); - | 4: Item.makeConst(x, Sym.tBool, I64(rnd(2))); - | 5: return Item.makeItem(x, strs[rnd(3)]); - | 6: Item.makeConst(x, Sym.tNil, 0); - | 7: return Item.makeItem(x, procs[rnd(6)]); - | 8: if rnd(4) = 0 then - match rnd(3) of - | 0: return Item.makeItem(x, typeObj); - | 1: return Item.makeItem(x, builtinObj); - | else: return Item.makeItem(x, caseObj); - end; - end; - return Item.makeItem(x, arrConst); - | 13: try Item.makeItem(x, vars[rnd(NumT)]); return Item.value(x); - | else: return varItem(x, ty[rnd(NumT)]); - end; - return nil; - end; - k := rnd(10); - if Sym.isInt(t) then - if k < 3 then Item.makeConst(x, Sym.tConst, constVal() mod 100); return nil; end; - if k = 3 then Item.makeConst(x, t, constVal() mod 100); return nil; end; - elsif t^.form = Sym.fReal then - if k < 3 then r := F64(constVal() mod 1000) / 8.0; Item.makeReal(x, r); return nil; end; - elsif t^.form = Sym.fBool then - if k < 3 then Item.makeConst(x, Sym.tBool, I64(rnd(2))); return nil; end; - elsif t^.form = Sym.fPtr then - if k < 2 then Item.makeConst(x, Sym.tNil, 0); return nil; end; - elsif (t^.form = Sym.fSlice) and (t^.base = Sym.tU8) then - if k < 3 then return Item.makeItem(x, strs[rnd(3)]); end; - end; - return varItem(x, t); -end; - -(* A condition, left as one: a comparison of two integers *) -proc condItem(var x: Item.Item): ERROR; -var y: Item.Item; L: INT; op: Scan.Sym; t: ^Sym.Type; -begin - t := intType(); - op := Cmps[rnd(6)]; - try operand(x, t); - try Item.op1(op, x, L); - try operand(y, t); - return Item.op2(op, x, y, L); -end; - -(* A variable, or part of one: through a pointer, a field, an element, a slice *) -proc place(var x: Item.Item): ERROR; -var i, k: INT; f: ^Sym.Obj; y, lo, hi: Item.Item; -begin - try varItem(x, ty[rnd(NumT)]); - for i := 1 to 3 do - if rnd(3) # 0 then - k := rnd(6); - if (k = 0) or (x.typ^.form = Sym.fPtr) then - try Item.deref(x); - elsif x.typ^.form = Sym.fRecord then - f := x.typ^.fields; - k := rnd(3); - while (k > 0) and (f^.next # nil) do f := f^.next; DEC(k); end; - try Item.field(x, f); - elsif (x.typ^.form in {Sym.fArray, Sym.fSlice, Sym.fStr}) then - try Item.indexPrep(x); - if k < 4 then - try operand(y, intType()); - try Item.index(x, y); - else - try operand(lo, intType()); - try Item.sliceLo(lo); - try operand(hi, intType()); - try Item.slice(x, lo, hi); - end; - end; - end; - end; - return nil; -end; - -(* The state of an item *) -proc dump(var x: Item.Item); -var name: []U8; flags: INT; -begin - name := "-"; - if x.typ # nil then name := Sym.typeName(x.typ); end; - flags := bit(x.vol) + 2 * bit(x.isVar) + 4 * bit(x.odd) + 8 * bit(x.ro) + 16 * bit(x.deref); - Fmt.print("I {} {} {} {} {} {} {}", [Fmt.int(INT(x.mode)), Fmt.str(name), Fmt.int(INT(x.rel)), - Fmt.int(bit(x.relSigned)), Fmt.int(x.fj), Fmt.int(x.tj), Fmt.int(x.val)]); - Fmt.print(" {} {} {} {} depth {} pushed {}\n", [Fmt.int(INT(x.base)), Fmt.int(x.chunk), Fmt.int(x.off), - Fmt.int(flags), Fmt.int(Gen.depth), Fmt.int(Gen.pushed)]); -end; - -(* A call: of a procedure of the script or a variable of a procedure - type, with arguments that mostly fit its parameters *) -proc doCall(var x: Item.Item): ERROR; -var f, a: Item.Item; p: ^Sym.Obj; n: INT; -begin - f := {}; - f.pos := pos(); - if rnd(5) = 0 then try Item.makeItem(f, vars[YProc0 + rnd(3)]); - else try Item.makeItem(f, procs[rnd(6)]); end; - try Item.beginCall(call, f); - p := call.param; - n := rnd(4) - 1; - while (p # nil) or (n > 0) do - if p # nil then - if p^.isVar and (rnd(2) = 0) then - try varItem(a, ty[YVptr]); - try Item.deref(a); - elsif rnd(6) = 0 then try operand(a, nil); - else try operand(a, p^.typ); end; - p := p^.next; - else - try operand(a, nil); - n := n - 1; - end; - try Item.passArg(call, a); - if (p = nil) and (n < 0) then n := 0; end; - if (p # nil) and (n < 0) and (rnd(4) = 0) then p := nil; n := 0; end; - end; - return Item.endCall(call, x); -end; - -(* A binary operation, with a condition's jumps made after it, sometimes *) -proc binary(var x: Item.Item): ERROR; -var y: Item.Item; L, M: INT; op: Scan.Sym; t: ^Sym.Type; -begin - L := Gen.NoLabel; - M := Gen.NoLabel; - op := Ops[rnd(20)]; - match rnd(8) of - | 0, 1: t := intType(); - | 2: t := ty[YReal + rnd(2)]; - | 3: t := ty[YBool]; - | 4: t := ty[YEnum + rnd(2)]; - | else: t := ty[rnd(NumT)]; - end; - if rnd(6) = 0 then - (* and, or *) - if flag() then op := Scan.sAnd; else op := Scan.sOr; end; - try condItem(x); - try Item.op1(op, x, L); - try condItem(y); - elsif op = Scan.sIn then - try operand(x, ty[YEnum]); - try Item.op1(op, x, L); - try operand(y, ty[YSet]); - else - try operand(x, t); - try Item.op1(op, x, L); - if rnd(5) = 0 then try operand(y, nil); else try operand(y, t); end; - end; - try Item.op2(op, x, y, L); - if (x.mode = Item.mCond) and (rnd(3) # 0) then - if flag() then try Item.condJump(x, M); else try Item.condJumpTrue(x, M); end; - Gen.fix(M); - x.mode := Item.mNone; (* its values are used *) - end; - return nil; -end; - -proc unary(var x: Item.Item): ERROR; -begin - try operand(x, nil); - match rnd(3) of - | 0: return Item.neg(x); - | 1: return Item.notOp(x); - | else: return Item.complement(x); - end; -end; - -(* match: the slot of a variant, an enumeration or an integer, and - tests of it *) -proc doMatch(var x: Item.Item): ERROR; -var p, lo, hi: I64; L: INT; -begin - L := Gen.NoLabel; - match rnd(3) of - | 0: try varItem(x, ty[YVariant]); - | 1: try varItem(x, ty[YEnum]); - | else: try operand(x, nil); - end; - p := try Item.matchBegin(x); - Item.matchTag(p, constVal() mod 10, L); - lo := constVal() mod 10; - hi := constVal() mod 10 + 10; - Item.matchRange(p, lo, hi, L); - Item.matchRange(p, 3, 3, L); - Gen.fix(L); - return nil; -end; - -(* A for loop's variable stepped and tested *) -proc doFor(var x: Item.Item): ERROR; -var o: ^Sym.Obj; lim: Item.Item; step: I64; exitL: INT; -begin - o := vars[IntTypes[rnd(8)]]; - step := constVal() mod 5; - if step = 0 then step := 1; end; - if rnd(2) = 0 then step := -step; end; - lim := {}; - lim.pos := pos(); - Item.makeConst(lim, o^.typ, constVal() mod 50); - exitL := Gen.NoLabel; - try Item.forStep(o, step); - try Item.forNext(o, lim, step, exitL, Chunk.pc, Chunk.pc); - Gen.fix(exitL); - x := lim; - return nil; -end; - -(* INC and DEC, pin, COPY *) -proc doMisc(var x: Item.Item): ERROR; -var y: Item.Item; -begin - match rnd(5) of - | 0: - try place(x); - try Item.address(x); - try operand(y, x.typ); - return Item.incDec(x, y, flag()); - | 1: - try place(x); - return Item.pin(x); - | 2: - try place(x); - try Item.copyDest(x); - try place(y); - return Item.copyFrom(x, y); - | 3: - try operand(x, nil); - Item.makeStr(y); - return Item.coerce(y, ty[YBytes]); - | else: - try operand(x, nil); - Item.nilTest(x); - Item.usesReals(x.pos); - return Item.discard(x); - end; -end; - -(* A variant, a record's fields, an error's chunk, the message of one *) -proc doTemp(var x: Item.Item): ERROR; -var a: Item.Item; c, h, n: INT; f: ^Sym.Obj; -begin - x := {}; - x.pos := pos(); - match rnd(5) of - | 0: Item.zeroTemp(x, ty[YVariant]); - | 1: - Item.construct(x, caseObj); - f := caseObj^.fields; - try operand(a, f^.typ); - return Item.setField(x, f, a); - | 2: - Item.zeroTemp(x, ty[YRec]); - f := ty[YRec]^.fields; - if flag() then f := f^.next; end; - try operand(a, f^.typ); - return Item.setField(x, f, a); - | 3: - try operand(a, nil); - h := rnd(3) - 1; - n := rnd(3) - 1; - c := try Item.errorChunk(a, h, n); - Item.makeConst(x, Sym.tError, I64(c) + 1); - return nil; - | else: - try operand(x, ty[YErr]); - return Item.msg(x); - end; - return nil; -end; - -proc doReturn(var x: Item.Item): ERROR; -var f: ^Sym.Obj; -begin - if (curRes = 4) and flag() then - (* several results *) - f := tuple2^.fields; - if flag() then f := f^.next; end; - if flag() then - try operand(x, f^.typ); - return Item.returnField(f, x); - end; - try doCall(x); - return Item.returnAll(x); - end; - if rnd(4) = 0 then return Item.returnNone(); end; - try operand(x, results[curRes]); - return Item.returnValue(x); -end; - -(* One step of the script, whose item is x *) -proc step(k: INT; var x: Item.Item): ERROR; -var y: Item.Item; -begin - x := {}; - match k of - | 0: try operand(x, nil); return Item.value(x); - | 1: try operand(x, nil); return Item.address(x); - | 2: try operand(x, nil); return Item.coerce(x, ty[rnd(NumT)]); - | 3: try operand(x, nil); return Item.coerceExact(x, intType(), ""); - | 4: - try place(x); - try operand(y, x.typ); - return Item.assign(x, y); - | 5: return unary(x); - | 6: return binary(x); - | 7: try operand(x, nil); return Item.convert(x, ty[rnd(NumT)]); - | 8: return place(x); - | 9: return doCall(x); - | 10: return doMatch(x); - | 11: return doFor(x); - | 12: return doMisc(x); - | 13: return doTemp(x); - | 14: return doReturn(x); - | else: return binary(x); - end; -end; - -(* One procedure of Steps steps *) -proc genProc(c: INT); -var - sig: ^Sym.Type; i, k, line: INT; x: Item.Item; err: ERROR; -begin - curRes := rnd(9); - sig := procType(results[curRes], nil, 0); - Item.beginProc(sig, nil); - for i := 0 to 5 do _ := Gen.allocPerm(8, 8); end; - makeVars(); - Item.setChecks(flag()); - line := 1 + rnd(40); - Gen.enterBody(c, line); - for i := 1 to Steps do - if Diag.errors >= 19 then break; end; (* the 20th would end the run *) - k := rnd(17); - Item.setDemoted(false); - err := step(k, x); - if err # nil then - Fmt.print("S {} {} failed\n", [Fmt.int(c), Fmt.int(k)]); - else - Fmt.print("S {} {} ok\n", [Fmt.int(c), Fmt.int(k)]); - dump(x); - (* the items of these steps are as an operand is, on the stack *) - if (k < 4) or ((k >= 5) and (k <= 9)) then _ := Item.discard(x); end; - end; - Fmt.print("demoted {}\n", [Fmt.int(bit(Item.demoted))]); - Gen.resetStack(0, 0); - end; - _ := Gen.endProc(c, sig^.base # nil); -end; - -proc chunkNameOf(i: INT): INT; -var s: [3]U8; -begin - s[0] := 'p'; s[1] := U8(48 + i div 10); s[2] := U8(48 + i mod 10); - return Scan.intern(ADR(s[0]), 3); -end; - -proc main*; -var i, c: INT; path: []U8; arg: []U8; -begin - arg := Files.arg(1); - seed := 5; - for i := 0 to LEN(arg) - 1 do seed := seed * 10 + I64(arg[i]) - 48; end; - Letters := "abcdefgh\0"; - path := "src/test/ItemDump.txt"; - if LEN(Scan.open(path)) # 0 then Diag.fatal("no file"); end; - Scan.next; (* the string the file starts with *) - X.setListing(true); - Chunk.setCurFile(Scan.intern(ADR(path[0]), LEN(path))); - for i := 0 to NumProcs - 1 do c := Chunk.newChunk(Chunk.secText, -1, chunkNameOf(i)); Chunk.setLive(c); end; - for i := 0 to NumGlobs - 1 do - c := Chunk.newChunk(Chunk.secBss, -1, chunkNameOf(NumProcs + i)); - Chunk.setBss(c, 64, 8); - end; - makeTypes(); - makeObjs(); - for i := 0 to NumProcs - 1 do genProc(FirstProc + i); end; - Fmt.print("pc {} fixups {} lines {} said {}\n", [Fmt.int(Chunk.pc), - Fmt.int(Chunk.numFixups), Fmt.int(Chunk.numLines), Fmt.int(X.saidCount())]); - for i := 0 to Chunk.pc - 1 do Out.hex(U64(Chunk.text[i]), 2); end; - Out.ln; - for i := 0 to Chunk.numFixups - 1 do - Fmt.print("F {} {} {}\n", [Fmt.int(Chunk.fixups[i].at), Fmt.int(Chunk.fixups[i].chunk), Fmt.int(Chunk.fixups[i].addend)]); - end; - for i := 0 to Chunk.numLines - 1 do - Fmt.print("L {} {} {} {}\n", [Fmt.int(Chunk.lines[i].at), Fmt.int(Chunk.lines[i].chunk), - Fmt.int(Chunk.lines[i].line), Fmt.int(Chunk.lines[i].col)]); - end; - X.list(-1); -end; - -end ItemDump. diff --git a/src/test/ItemDump.txt b/src/test/ItemDump.txt deleted file mode 100644 index 5be0a4c..0000000 --- a/src/test/ItemDump.txt +++ /dev/null @@ -1,41 +0,0 @@ -"item test" - x value sum alpha beta; - next beta items len alpha next count alpha beta; - value beta count beta next value; - len beta count; - sum len alpha len len value alpha count; - next x n; - x next beta len n next; - sum x beta len len sum count items beta; - tmp beta len alpha len count p; - next value items p len p items n; - x tmp count beta; - n next p items tmp p n; - beta beta next value x items x; - value alpha sum beta next len; - items items tmp items len p len p beta; - beta n p tmp sum beta alpha tmp tmp; - sum len sum p n; - value sum items alpha p items x len; - p alpha count; - n x tmp count value value p beta x; - value next n x value next; - tmp value items sum value; - x beta x x; - sum count alpha p; - len x n n alpha x value next items; - len items x tmp next len sum; - tmp alpha p sum next value value value; - beta p sum value alpha count; - count p x; - items len alpha; - alpha len x; - beta items len alpha beta count len; - x sum n items len items; - beta beta p p p p; - beta x beta tmp items; - n p tmp x next alpha count next; - x tmp next alpha next; - sum beta tmp n next; - x items count next next; - next items sum count len count count value tmp; diff --git a/src/test/LinkDump.pas b/src/test/LinkDump.pas deleted file mode 100644 index 57a628b..0000000 --- a/src/test/LinkDump.pas +++ /dev/null @@ -1,91 +0,0 @@ -{ Makes a program of chunks by a fixed pseudo-random script, links it and - writes it as an executable to the file of the second argument, in the - way the first says (0 static, 1 dynamic, 2 stripped): the seed's side - of a test of src/Link.raz and src/Elf.raz, whose LinkDump.raz does - the same and writes the same file. } -program linkdump; -{$mode objfpc}{$H-} -uses diag, scan, seedcontinue, chunk, link, elf; -const NumC = 200; -var - seed: int64; - names: array[0..7] of longint; - i, j, k, kind, n, x, y, z, mode: longint; - b: array[0..63] of byte; - -function Rnd(n: longint): longint; -begin - seed := (seed * 1103515245 + 12345) and $7FFFFFFF; - Rnd := (seed shr 8) mod n; -end; - -var c, e, line, col: longint; -begin - seed := 5; - mode := ord(ParamStr(1)[1]) - 48; - for i := 0 to 7 do begin b[0] := 97 + i; names[i] := Intern(@b[0], 1) end; - placesChunk := NewChunk(secRodata, -1, -1); - SetData(placesChunk, @b[0], 0, 8); - for i := 0 to NumC - 1 do begin - if i = 0 then kind := 0 else kind := Rnd(10); - x := Rnd(4); y := Rnd(8); - case kind of - 0..5: begin - c := NewChunk(secText, x - 1, y); - BeginChunk(c); - z := Rnd(3); curFile := names[z]; - line := Rnd(300); col := Rnd(60); - NoteLine(line, col); - if i = 0 then begin AddFixup(pc, placesChunk, 0); Put4(0) end; - n := 2 + Rnd(10); - for j := 1 to n do begin - z := Rnd(5); - if z <= 1 then begin - e := Rnd(NumC); y := Rnd(8); - AddFixup(pc, e, y); - Put4(0); - end else if z = 2 then begin - z := Rnd(3); curFile := names[z]; - line := Rnd(300); col := Rnd(60); - NoteLine(line, col); - end else begin x := Rnd(1 shl 30); Put4(x); x := Rnd(256); Put1(x) end; - end; - EndChunk(c); - n := Rnd(3); - for j := 1 to n do begin - x := Rnd(8); y := Rnd(200); z := Rnd(6); - NoteVar(c, names[x], y - 100, z); - end; - end; - 6, 7: begin - c := NewChunk(secRodata, x - 1, y); - n := 8 * (1 + Rnd(4)); - for j := 0 to n - 1 do b[j] := Rnd(256); - SetData(c, @b[0], n, 8); - for j := 0 to n div 8 - 1 do begin - z := Rnd(2); - if z = 0 then begin e := Rnd(NumC); y := Rnd(8); AddDataFixup(c, j * 8, e, y) end; - end; - end; - 8: begin - c := NewChunk(secBss, x - 1, y); - z := Rnd(200); e := Rnd(4); - SetBss(c, 1 + z, 1 shl e); - end; - 9: begin z := Rnd(8); c := ExternChunk(names[z]) end; - end; - end; - { every chunk a fixup names must exist, and be defined if it is reached } - while numChunks < NumC do begin c := NewChunk(secBss, -1, -1); SetBss(c, 8, 8) end; - for i := 0 to numChunks - 1 do - if chunks^[i].size < 0 then SetBss(i, 8, 8); - mainPath := 'some/main.raz'; - if mode = 1 then begin - numNeeded := 2; - needed[0] := names[0]; needed[1] := names[1]; - end; - stripped := mode = 2; - if mode = 2 then placesChunk := -1; - MarkLive(1); - WriteElf(ParamStr(2), 1); -end. diff --git a/src/test/LinkDump.raz b/src/test/LinkDump.raz deleted file mode 100644 index 92d97de..0000000 --- a/src/test/LinkDump.raz +++ /dev/null @@ -1,103 +0,0 @@ -(* Makes a program of chunks by a fixed pseudo-random script, links it - and writes it as an executable to the file of the second argument, - in the way the first says (0 static, 1 dynamic, 2 stripped): - src/Link.raz's and src/Elf.raz's side of a test against the seed's, - LinkDump.pas, which writes the same file. *) -module LinkDump; -import Files, Diag, Scan, Chunk, Link, Elf; - -const NumC = 200; - -var seed: I64; - -proc rnd(n: INT): INT; -begin - seed := (seed * 1103515245 + 12345) & 7FFFFFFFH; - return INT((seed shr 8) mod I64(n)); -end; - -(* a library that must be taken *) -proc ok(taken: BOOL); -begin - if not taken then Diag.fatal("too many libraries"); end; -end; - -proc main*; -var - names: [8]INT; b: [64]U8; arg: []U8; - i, j, kind, n, x, y, z, c, e, line, col, mode: INT; -begin - seed := 5; - arg := Files.arg(1); - mode := INT(arg[0]) - 48; - for i := 0 to 7 do b[0] := U8(97 + i); names[i] := Scan.intern(ADR(b[0]), 1); end; - Link.setPlaces(Chunk.newChunk(Chunk.secRodata, -1, -1)); - Chunk.setData(Link.placesChunk, ADR(b[0]), 0, 8); - for i := 0 to NumC - 1 do - if i = 0 then kind := 0; else kind := rnd(10); end; - x := rnd(4); y := rnd(8); - match kind of - | 0..5: - c := Chunk.newChunk(Chunk.secText, x - 1, y); - Chunk.beginChunk(c); - z := rnd(3); Chunk.setCurFile(names[z]); - line := rnd(300); col := rnd(60); - Chunk.noteLine(line, col); - if i = 0 then Chunk.addFixup(Chunk.pc, Link.placesChunk, 0); Chunk.put4(0); end; - n := 2 + rnd(10); - for j := 1 to n do - z := rnd(5); - if z <= 1 then - e := rnd(NumC); y := rnd(8); - Chunk.addFixup(Chunk.pc, e, y); - Chunk.put4(0); - elsif z = 2 then - z := rnd(3); Chunk.setCurFile(names[z]); - line := rnd(300); col := rnd(60); - Chunk.noteLine(line, col); - else - x := rnd(1 shl 30); Chunk.put4(x); x := rnd(256); Chunk.put1(x); - end; - end; - Chunk.endChunk(c); - n := rnd(3); - for j := 1 to n do - x := rnd(8); y := rnd(200); z := rnd(6); - Chunk.noteVar(c, names[x], y - 100, z); - end; - | 6, 7: - c := Chunk.newChunk(Chunk.secRodata, x - 1, y); - n := 8 * (1 + rnd(4)); - for j := 0 to n - 1 do b[j] := U8(rnd(256)); end; - Chunk.setData(c, ADR(b[0]), n, 8); - for j := 0 to n div 8 - 1 do - z := rnd(2); - if z = 0 then e := rnd(NumC); y := rnd(8); Chunk.addDataFixup(c, j * 8, e, y); end; - end; - | 8: - c := Chunk.newChunk(Chunk.secBss, x - 1, y); - z := rnd(200); e := rnd(4); - Chunk.setBss(c, 1 + z, 1 shl e); - | else: z := rnd(8); c := Chunk.externChunk(names[z]); - end; - end; - (* every chunk a fixup names must exist, and be defined if it is reached *) - while Chunk.numChunks < NumC do - c := Chunk.newChunk(Chunk.secBss, -1, -1); - Chunk.setBss(c, 8, 8); - end; - for i := 0 to Chunk.numChunks - 1 do - if Chunk.chunks[i].size < 0 then Chunk.setBss(i, 8, 8); end; - end; - Elf.setMainPath("some/main.raz"); - if mode = 1 then - ok(Elf.needLib(names[0])); - ok(Elf.needLib(names[1])); - end; - Elf.setStripped(mode = 2); - if mode = 2 then Link.setPlaces(-1); end; - Link.markLive(1); - Elf.writeElf(Files.arg(2), 1); -end; - -end LinkDump. diff --git a/src/test/ScanBench.pas b/src/test/ScanBench.pas index f3bab1d..75b0336 100644 --- a/src/test/ScanBench.pas +++ b/src/test/ScanBench.pas @@ -2,7 +2,7 @@ against src/Scan.raz (ScanBench.raz). } program scanbench; {$mode objfpc}{$H-} -uses diag, scan, seedcontinue; +uses diag, scan; var n: longint; begin if Open(ParamStr(1)) <> '' then Fatal('no file'); diff --git a/src/test/ScanDump.pas b/src/test/ScanDump.pas deleted file mode 100644 index 6029094..0000000 --- a/src/test/ScanDump.pas +++ /dev/null @@ -1,21 +0,0 @@ -{ Prints the symbols of a file, one per line: the seed's side of a test - of src/Scan.raz, which prints the same. A real is shown by its bits, - so that no formatting library decides what is the same. } -program scandump; -{$mode objfpc}{$H-} -uses diag, scan, seedcontinue; -var i: longint; bits: int64; -begin - if Open(ParamStr(1)) <> '' then Fatal('no file'); - repeat - Next; - write(LineOf(TokPos), ':', TokPos.a, ':', TokPos.b, ' ', CurLine, ':', CurCol, ' ', ord(tok)); - case tok of - sIdent: write(' ', NameOf(id), ' ', id); - sInt, sChar: write(' ', ival); - sReal: begin Move(rval, bits, 8); write(' ', HexStr(bits, 16)) end; - sString: begin write(' "'); for i := 0 to slen - 1 do write(sbuf[i], ','); write('"') end; - end; - writeln; - until tok = sEof; -end. diff --git a/src/test/ScanDump.raz b/src/test/ScanDump.raz deleted file mode 100644 index c5cf657..0000000 --- a/src/test/ScanDump.raz +++ /dev/null @@ -1,45 +0,0 @@ -(* Prints the symbols of a file, one per line: src/Scan.raz's side of a - test against the seed's, ScanDump.pas, which prints the same. The - symbols are numbered as the seed numbers them: it has no continue - (report 2026-10.31), the last reserved word here, so its end of the - file is one lower. A file with continue in it isn't compared. *) -module ScanDump; -import Out, Fmt, Files, Diag, Scan; - -(* s as the seed numbers it *) -proc seedSym(s: Scan.Sym): INT; -begin - if s = Scan.sEof then return INT(s) - 1; end; - return INT(s); -end; - -proc main*; -var p: Diag.Pos; bits: ^U64; i: INT; str: []U8; r: F64; -begin - if LEN(Scan.open(Files.arg(1))) # 0 then Diag.fatal("no file"); end; - repeat - Scan.next; - p := Scan.tokPos(); - Fmt.print("{}:{}:{} {}:{} {}", [Fmt.int(Diag.lineOf(p)), Fmt.int(p.a), Fmt.int(p.b), - Fmt.int(Scan.curLine()), Fmt.int(Scan.curCol()), Fmt.int(seedSym(Scan.tok))]); - match Scan.tok of - | sIdent: Fmt.print(" {} {}", [Fmt.str(Scan.nameOf(Scan.id)), Fmt.int(Scan.id)]); - | sInt: Fmt.print(" {}", [Fmt.int(Scan.ival)]); - | sChar: Fmt.print(" {}", [Fmt.int(Scan.ival)]); - | sReal: - r := Scan.rval; - bits := ^U64(ADR(r)); - Out.str(" "); - Out.hex(bits^, 16); - | sString: - Out.str(" \""); - str := Scan.strBytes(); - for i := 0 to LEN(str) - 1 do Fmt.print("{},", [Fmt.int(INT(str[i]))]); end; - Out.str("\""); - | else: - end; - Out.ln; - until Scan.tok = Scan.sEof; -end; - -end ScanDump. diff --git a/src/test/SymDump.pas b/src/test/SymDump.pas deleted file mode 100644 index ef2faeb..0000000 --- a/src/test/SymDump.pas +++ /dev/null @@ -1,185 +0,0 @@ -{ Builds types and objects by a fixed pseudo-random script, asks of them - what the parser asks of sym, and prints the answers: the seed's side - of a test of src/Sym.raz, whose SymDump.raz does the same and prints - the same. With the argument dup or deep it ends in the error of a name - declared twice, or of too many scopes. } -program symdump; -{$mode objfpc}{$H-} -uses diag, scan, seedcontinue, sym; -const - Words: array[0..19] of string = ('alpha', 'alpho', 'beta', 'bet', 'gamma', 'gama', 'delta', 'Delta', - 'eps', 'zeta', 'eta', 'theta', 'iota', 'kappa', 'lambda', 'mu', 'nu', 'xi', 'omicron', 'pi'); -var - seed: int64; - names: array[0..19] of longint; - mods: array[0..2] of PModule; - base: array[0..11] of PType; - pool: array[0..299] of PType; - objs: array[0..299] of PObj; - numPool, numObjs, i, k, n, op, x, y, z: longint; - t, o2: PType; - o: PObj; - saved: array[0..MaxDepth - 1] of longint; - d: longint; - v: int64; - -function Rnd(n: longint): longint; -begin - seed := (seed * 1103515245 + 12345) and $7FFFFFFF; - Rnd := (seed shr 8) mod n; -end; - -function Fields(m: longint; allowVar: boolean): PObj; -var f, first, last: PObj; j, x, y, z: longint; -begin - first := nil; last := nil; - for j := 1 to m do begin - x := Rnd(20); y := Rnd(numPool); - f := NewObj(oField, names[x], pool[y]); - z := Rnd(2); f^.exported := z = 0; - z := Rnd(3); f^.isVar := allowVar and (z = 0); - f^.val := j * 8; - if first = nil then first := f else last^.next := f; - last := f; - end; - Fields := first; -end; - -procedure Add(t: PType); -begin - if numPool <= High(pool) then begin pool[numPool] := t; inc(numPool) end; -end; - -procedure MakeType; -var t: PType; r, m, x: longint; f: PObj; -begin - r := Rnd(11); - case r of - 0: t := NewType(fPtr, 8, 8); - 1: t := NewType(fArray, 0, 1); - 2: t := NewType(fSlice, 16, 8); - 3: t := NewType(fRecord, 0, 8); - 4: t := NewType(fProc, 8, 8); - 5: t := NewType(fSet, 8, 8); - 6: t := NewType(fVariant, 16, 8); - 7: t := NewType(fEnum, 1, 1); - 8: t := NewType(fRecord, 16, 8); - 9: t := NewType(fProc, 8, 8); - else t := NewType(fPtr, 8, 8); - end; - x := Rnd(numPool); t^.base := pool[x]; - t^.len := Rnd(5); - x := Rnd(4); t^.vol := x = 0; - x := Rnd(4); t^.variadic := (t^.form = fProc) and (x = 0); - x := Rnd(2); t^.tuple := (t^.form = fRecord) and (x = 0); - if t^.form in [fRecord, fProc, fVariant] then begin - m := Rnd(4); - t^.fields := Fields(m, t^.form = fProc); - t^.len := m; - if t^.form = fRecord then t^.base := nil; - x := Rnd(2); - if (t^.form = fProc) and (x = 0) then t^.base := nil; - if t^.form = fVariant then begin - f := t^.fields; - while f <> nil do begin f^.cls := oCase; x := Rnd(3); f^.fields := Fields(x, false); f := f^.next end; - end; - end; - x := Rnd(3); - if x = 0 then begin - x := Rnd(20); t^.obj := NewObj(oType, names[x], t); - x := Rnd(3); t^.obj^.module := mods[x]; - end; - Add(t); -end; - -begin - seed := 11; - for i := 0 to 19 do names[i] := Intern(@Words[i][1], length(Words[i])); - for i := 0 to 2 do begin - New(mods[i]); FillChar(mods[i]^, SizeOf(TModule), 0); - mods[i]^.sname := names[i * 5]; - end; - curModule := mods[0]; - base[0] := tI8; base[1] := tI16; base[2] := tI32; base[3] := tI64; base[4] := tU8; - base[5] := tU16; base[6] := tU32; base[7] := tU64; base[8] := tBool; base[9] := tF32; - base[10] := tReal; base[11] := tInt; - for i := 0 to 11 do Add(base[i]); - if ParamCount >= 1 then begin if Open('lib/Rt.raz') <> '' then Fatal('no file'); Next; Next end; - numObjs := 0; - for k := 1 to 4000 do begin - op := Rnd(14); - case op of - 0, 1, 2: MakeType; - 3, 4: if numObjs <= High(objs) then begin - x := Rnd(8); y := Rnd(20); - if Find(x, names[y]) = nil then begin - z := Rnd(8); n := Rnd(numPool); - o := NewObj(TClass(z), names[y], pool[n]); - z := Rnd(2); o^.exported := z = 0; - z := Rnd(3); o^.module := mods[z]; - Insert(o, x); - objs[numObjs] := o; inc(numObjs); - writeln('I ', x, ' ', NameOf(names[y]), ' ', o^.scope); - end; - end; - 5: begin x := Rnd(8); y := Rnd(20); o := Find(x, names[y]); - write('F ', x, ' ', NameOf(names[y]), ' '); - if o = nil then writeln('-') else writeln(ord(o^.cls), ' ', o^.scope); - end; - 6: begin - x := Rnd(8); y := Rnd(20); - if ScopeDepth < 6 then OpenScope(x); - o := Lookup(names[y]); - write('L ', ScopeDepth, ' ', NameOf(names[y]), ' '); - if o = nil then writeln('-') else writeln(ord(o^.cls), ' ', o^.scope); - z := Rnd(2); - if (ScopeDepth > 1) and (z = 0) then CloseScope; - end; - 7: begin - y := Rnd(20); x := Rnd(9) - 1; z := Rnd(3); curModule := mods[z]; - o := Closest(names[y], x); - write('C ', NameOf(names[y]), ' ', x, ' '); - if o = nil then writeln('-') else writeln(NameOf(o^.name), ' ', o^.scope); - end; - 8: begin - x := Rnd(numPool); z := Rnd(3); curModule := mods[z]; - writeln('T ', TypeName(pool[x])); - end; - 9: begin - x := Rnd(numPool); y := Rnd(numPool); - writeln('S ', ord(Same(pool[x], pool[y])), ' ', ord(Same(pool[x], pool[x]))); - t := NewType(fPtr, 8, 8); t^.base := pool[x]; - o2 := t; t := NewType(fPtr, 8, 8); t^.base := pool[x]; - writeln('S ', ord(Same(o2, t))); - end; - 10: if numObjs > 0 then begin x := Rnd(numObjs); writeln('P ', HexStr(Fingerprint(objs[x]), 16)) end; - 11: begin - x := Rnd(numPool); y := Rnd(1 shl 20); z := Rnd(2); n := Rnd(2); - v := int64(y) * 4096 * z - y * n; - t := pool[x]; - writeln('B ', ord(Fits(v, t)), ' ', ord(IsInt(t)), ' ', ord(IsScalar(t)), ' ', ord(IsAggregate(t)), ' ', - ord(InTwoRegs(t)), ' ', ord(BehindPointer(t)), ' ', AlignUp(y, 1 shl z)); - end; - 12: if (ScopeDepth > 1) and (ScopeDepth < 6) then begin - d := ScopeDepth; SaveScopes(d, saved); - writeln('V ', ScopeDepth, ' ', TopScope); - RestoreScopes(d, saved); - writeln('V ', ScopeDepth, ' ', TopScope); - x := NewScope; writeln('N ', x); - end; - 13: begin x := ScopeDepth; if x > 3 then CutScopes(2); writeln('X ', x, ' ', ScopeDepth) end; - end; - end; - { the universe, and the sizes it made } - for i := 0 to 11 do writeln('U ', ord(base[i]^.form), ' ', base[i]^.size, ' ', ord(base[i]^.signed), ' ', NameOf(base[i]^.obj^.name)); - if ParamCount >= 1 then begin - if ParamStr(1) = 'dup' then begin - o := NewObj(oVar, names[3], tI8); - Insert(o, 100); Insert(o, 100); - end; - if ParamStr(1) = 'deep' then begin - CutScopes(1); - for i := 1 to MaxDepth + 1 do OpenScope(i); - end; - end; -end. diff --git a/src/test/SymDump.raz b/src/test/SymDump.raz deleted file mode 100644 index 8807ff7..0000000 --- a/src/test/SymDump.raz +++ /dev/null @@ -1,225 +0,0 @@ -(* Builds types and objects by a fixed pseudo-random script, asks of them - what the parser asks of Sym, and prints the answers: src/Sym.raz's - side of a test against the seed's, SymDump.pas, which prints the same. - With the argument dup or deep it ends in the error of a name declared - twice, or of too many scopes. *) -module SymDump; -import Out, Fmt, Files, Heap, Diag, Scan, Sym; - -const - Words: [20][]U8 = ["alpha", "alpho", "beta", "bet", "gamma", "gama", "delta", "Delta", - "eps", "zeta", "eta", "theta", "iota", "kappa", "lambda", "mu", "nu", "xi", "omicron", "pi"]; - -var - seed: I64; - names: [20]INT; - mods: [3]^Sym.Module; - base: [12]^Sym.Type; - pool: [300]^Sym.Type; - objs: [300]^Sym.Obj; - classes: [8]Sym.Class; - numPool, numObjs: INT; - -proc rnd(n: INT): INT; -begin - seed := (seed * 1103515245 + 12345) & 7FFFFFFFH; - return INT((seed shr 8) mod I64(n)); -end; - -proc bit(f: BOOL): INT; -begin - if f then return 1; end; - return 0; -end; - -proc nm(i: INT): []U8; -begin - return Scan.nameOf(names[i]); -end; - -(* The result of a call that must succeed *) -proc ok(e: ERROR); -begin - if e # nil then Diag.finish; end; -end; - -proc fields(m: INT; allowVar: BOOL): ^Sym.Obj; -var f, first, last: ^Sym.Obj; j, x, y, z: INT; -begin - first := nil; last := nil; - for j := 1 to m do - x := rnd(20); y := rnd(numPool); - f := Sym.newObj(Sym.oField, names[x], pool[y]); - z := rnd(2); f^.exported := z = 0; - z := rnd(3); f^.isVar := allowVar and (z = 0); - f^.val := j * 8; - if first = nil then first := f; else last^.next := f; end; - last := f; - end; - return first; -end; - -proc add(t: ^Sym.Type); -begin - if numPool < 300 then pool[numPool] := t; INC(numPool); end; -end; - -proc makeType; -var t: ^Sym.Type; r, m, x: INT; f: ^Sym.Obj; -begin - r := rnd(11); - match r of - | 0: t := Sym.newType(Sym.fPtr, 8, 8); - | 1: t := Sym.newType(Sym.fArray, 0, 1); - | 2: t := Sym.newType(Sym.fSlice, 16, 8); - | 3: t := Sym.newType(Sym.fRecord, 0, 8); - | 4: t := Sym.newType(Sym.fProc, 8, 8); - | 5: t := Sym.newType(Sym.fSet, 8, 8); - | 6: t := Sym.newType(Sym.fVariant, 16, 8); - | 7: t := Sym.newType(Sym.fEnum, 1, 1); - | 8: t := Sym.newType(Sym.fRecord, 16, 8); - | 9: t := Sym.newType(Sym.fProc, 8, 8); - | else: t := Sym.newType(Sym.fPtr, 8, 8); - end; - x := rnd(numPool); t^.base := pool[x]; - t^.len := rnd(5); - x := rnd(4); t^.vol := x = 0; - x := rnd(4); t^.variadic := (t^.form = Sym.fProc) and (x = 0); - x := rnd(2); t^.tuple := (t^.form = Sym.fRecord) and (x = 0); - if (t^.form = Sym.fRecord) or (t^.form = Sym.fProc) or (t^.form = Sym.fVariant) then - m := rnd(4); - t^.fields := fields(m, t^.form = Sym.fProc); - t^.len := m; - if t^.form = Sym.fRecord then t^.base := nil; end; - x := rnd(2); - if (t^.form = Sym.fProc) and (x = 0) then t^.base := nil; end; - if t^.form = Sym.fVariant then - f := t^.fields; - while f # nil do - f^.cls := Sym.oCase; - x := rnd(3); f^.fields := fields(x, false); - f := f^.next; - end; - end; - end; - x := rnd(3); - if x = 0 then - x := rnd(20); t^.obj := Sym.newObj(Sym.oType, names[x], t); - x := rnd(3); t^.obj^.home := mods[x]; - end; - add(t); -end; - -proc main*; -var - i, k, op, x, y, z, n, d: INT; t, o2: ^Sym.Type; o: ^Sym.Obj; v: I64; - saved: [Sym.MaxDepth]INT; arg: []U8; -begin - seed := 11; - classes := [Sym.oConst, Sym.oType, Sym.oVar, Sym.oProc, Sym.oModule, Sym.oField, Sym.oCase, Sym.oBuiltin]; - for i := 0 to 19 do names[i] := Scan.intern(ADR(Words[i][0]), LEN(Words[i])); end; - for i := 0 to 2 do - NEW(mods[i], Heap.heap); - mods[i]^.sname := names[i * 5]; - end; - Sym.setCurModule(mods[0]); - base[0] := Sym.tI8; base[1] := Sym.tI16; base[2] := Sym.tI32; base[3] := Sym.tI64; base[4] := Sym.tU8; - base[5] := Sym.tU16; base[6] := Sym.tU32; base[7] := Sym.tU64; base[8] := Sym.tBool; base[9] := Sym.tF32; - base[10] := Sym.tReal; base[11] := Sym.tInt; - for i := 0 to 11 do add(base[i]); end; - arg := ""; - if ARGC > 1 then arg := Files.arg(1); end; - if LEN(arg) > 0 then - if LEN(Scan.open("lib/Rt.raz")) # 0 then Diag.fatal("no file"); end; - Scan.next; Scan.next; - end; - numObjs := 0; - for k := 1 to 4000 do - op := rnd(14); - match op of - | 0, 1, 2: makeType; - | 3, 4: - if numObjs < 300 then - x := rnd(8); y := rnd(20); - if Sym.find(x, names[y]) = nil then - z := rnd(8); n := rnd(numPool); - o := Sym.newObj(classes[z], names[y], pool[n]); - z := rnd(2); o^.exported := z = 0; - z := rnd(3); o^.home := mods[z]; - ok(Sym.insert(o, x)); - objs[numObjs] := o; INC(numObjs); - Fmt.print("I {} {} {}\n", [Fmt.int(x), Fmt.str(nm(y)), Fmt.int(o^.scope)]); - end; - end; - | 5: - x := rnd(8); y := rnd(20); o := Sym.find(x, names[y]); - Fmt.print("F {} {} ", [Fmt.int(x), Fmt.str(nm(y))]); - if o = nil then Out.str("-\n"); - else Fmt.print("{} {}\n", [Fmt.int(INT(o^.cls)), Fmt.int(o^.scope)]); end; - | 6: - x := rnd(8); y := rnd(20); - if Sym.scopeDepth() < 6 then ok(Sym.openScope(x)); end; - o := Sym.lookup(names[y]); - Fmt.print("L {} {} ", [Fmt.int(Sym.scopeDepth()), Fmt.str(nm(y))]); - if o = nil then Out.str("-\n"); - else Fmt.print("{} {}\n", [Fmt.int(INT(o^.cls)), Fmt.int(o^.scope)]); end; - z := rnd(2); - if (Sym.scopeDepth() > 1) and (z = 0) then Sym.closeScope; end; - | 7: - y := rnd(20); x := rnd(9) - 1; z := rnd(3); Sym.setCurModule(mods[z]); - o := Sym.closest(names[y], x); - Fmt.print("C {} {} ", [Fmt.str(nm(y)), Fmt.int(x)]); - if o = nil then Out.str("-\n"); - else Fmt.print("{} {}\n", [Fmt.str(Scan.nameOf(o^.name)), Fmt.int(o^.scope)]); end; - | 8: - x := rnd(numPool); z := rnd(3); Sym.setCurModule(mods[z]); - Fmt.print("T {}\n", [Fmt.str(Sym.typeName(pool[x]))]); - | 9: - x := rnd(numPool); y := rnd(numPool); - Fmt.print("S {} {}\n", [Fmt.int(bit(Sym.same(pool[x], pool[y]))), Fmt.int(bit(Sym.same(pool[x], pool[x])))]); - t := Sym.newType(Sym.fPtr, 8, 8); t^.base := pool[x]; - o2 := t; t := Sym.newType(Sym.fPtr, 8, 8); t^.base := pool[x]; - Fmt.print("S {}\n", [Fmt.int(bit(Sym.same(o2, t)))]); - | 10: - if numObjs > 0 then - x := rnd(numObjs); - Out.str("P "); Out.hex(Sym.fingerprint(objs[x]), 16); Out.ln; - end; - | 11: - x := rnd(numPool); y := rnd(1 shl 20); z := rnd(2); n := rnd(2); - v := I64(y) * 4096 * I64(z) - I64(y) * I64(n); - t := pool[x]; - Fmt.print("B {} {} {} {} {} {} {}\n", [Fmt.int(bit(Sym.fits(v, t))), Fmt.int(bit(Sym.isInt(t))), - Fmt.int(bit(Sym.isScalar(t))), Fmt.int(bit(Sym.isAggregate(t))), Fmt.int(bit(Sym.inTwoRegs(t))), - Fmt.int(bit(Sym.behindPointer(t))), Fmt.int(Sym.alignUp(y, 1 shl z))]); - | 12: - if (Sym.scopeDepth() > 1) and (Sym.scopeDepth() < 6) then - d := Sym.saveScopes(saved); - Fmt.print("V {} {}\n", [Fmt.int(Sym.scopeDepth()), Fmt.int(Sym.topScope())]); - Sym.restoreScopes(d, saved); - Fmt.print("V {} {}\n", [Fmt.int(Sym.scopeDepth()), Fmt.int(Sym.topScope())]); - x := Sym.newScope(); - Fmt.print("N {}\n", [Fmt.int(x)]); - end; - | else: - x := Sym.scopeDepth(); - if x > 3 then Sym.cutScopes(2); end; - Fmt.print("X {} {}\n", [Fmt.int(x), Fmt.int(Sym.scopeDepth())]); - end; - end; - for i := 0 to 11 do - Fmt.print("U {} {} {} {}\n", [Fmt.int(INT(base[i]^.form)), Fmt.int(base[i]^.size), - Fmt.int(bit(base[i]^.signed)), Fmt.str(Scan.nameOf(base[i]^.obj^.name))]); - end; - if arg = "dup" then - o := Sym.newObj(Sym.oVar, names[3], Sym.tI8); - ok(Sym.insert(o, 100)); - ok(Sym.insert(o, 100)); - end; - if arg = "deep" then - Sym.cutScopes(1); - for i := 1 to Sym.MaxDepth + 1 do ok(Sym.openScope(i)); end; - end; -end; - -end SymDump. diff --git a/src/test/X64Dump.pas b/src/test/X64Dump.pas deleted file mode 100644 index 8bcdea0..0000000 --- a/src/test/X64Dump.pas +++ /dev/null @@ -1,160 +0,0 @@ -{ Writes every instruction the encoder makes, with operands drawn by a - fixed pseudo-random script, then prints the bytes, the fixups and the - listing (raz -S) of what it made: the seed's side of a test of - src/X64.raz, whose X64Dump.raz does the same and prints the same. } -program x64dump; -{$mode objfpc}{$H-} -uses diag, scan, seedcontinue, chunk, x64; -var - seed: int64; - i, k, op, a, b, c, d, lab1, lab2: longint; - m: TMem; - path: string; - -function Rnd(n: longint): longint; -begin - seed := (seed * 1103515245 + 12345) and $7FFFFFFF; - Rnd := (seed shr 8) mod n; -end; - -{ a register, with the stack and base pointers and the high ones often enough } -function Reg: longint; -begin - Reg := Rnd(16); -end; - -{ a size of 1, 2, 4 or 8 bytes } -function Size: longint; -begin - Size := 1 shl Rnd(4); -end; - -{ a value that fits size bytes, of 8 as 32 bits } -function Imm(sz: longint): longint; -var x: longint; -begin - x := Rnd(1 shl 30); - case Rnd(3) of - 0: Imm := x mod 100 - 50; - 1: Imm := x - (1 shl 29); - else Imm := x mod 20000 - 10000; - end; - if sz = 1 then Imm := Imm mod 128 - else if sz = 2 then Imm := Imm mod 32768; -end; - -function RandMem: TMem; -var kind, base, idx, sc, disp, x: longint; -begin - kind := Rnd(6); - base := Reg; idx := Reg; sc := 1 shl Rnd(4); x := Rnd(5); - case x of - 0: disp := 0; - 1: disp := Rnd(250) - 125; - 2: disp := Rnd(1 shl 20) - (1 shl 19); - 3: disp := 127 + Rnd(3) - 1; - else disp := -128 + Rnd(3) - 1; - end; - case kind of - 0, 1: RandMem := Mem(base, disp); - 2: RandMem := MemRip(Rnd(5), disp); - 3, 4: RandMem := MemIdx(base, idx, sc, disp); - else RandMem := Mem(base, 0); - end; -end; - -begin - seed := 3; - path := 'lib/Rt.raz'; - if Open(path) <> '' then Fatal('no file'); - listing := true; - for i := 0 to 4 do NewChunk(secText, i - 1, i); - curFile := Intern(@path[1], length(path)); - BeginChunk(0); - lab1 := NoLabel; lab2 := NoLabel; - for k := 1 to 3000 do begin - if k mod 7 = 0 then begin a := Rnd(300); b := Rnd(60); NoteLine(1 + a, b) end; - op := Rnd(50); - case op of - 0: begin a := Reg; b := Reg; MovRR(a, b) end; - 1: begin - a := Reg; b := Rnd(6); c := Rnd(1 shl 30); - case b of - 0: MovRI(a, 0); - 1: MovRI(a, c); - 2: MovRI(a, -c); - 3: MovRI(a, int64(c) * 8 + 5); - 4: MovRI(a, int64(c) * 4096 * 4096 * 9 - 7); - else MovRI(a, 0); - end; - end; - 2: begin a := Rnd(8); m := RandMem; b := Size; c := Imm(b); AluMI(a, m, c, b) end; - 3: begin m := RandMem; b := Size; c := Imm(b); StoreI(m, c, b) end; - 4: begin a := Reg; m := RandMem; b := Size; c := Rnd(2); Load(a, m, b, c = 0) end; - 5: begin m := RandMem; a := Reg; b := Size; Store(m, a, b) end; - 6: begin a := Reg; m := RandMem; Lea(a, m) end; - 7: begin a := Rnd(8); b := Reg; c := Reg; Alu(a, b, c) end; - 8: begin a := Rnd(8); b := Reg; c := Imm(8); AluI(a, b, c) end; - 9: begin a := Rnd(8); b := Reg; m := RandMem; AluM(a, b, m) end; - 10: begin a := Reg; b := Reg; Imul(a, b) end; - 11: begin a := Reg; c := Imm(8); ImulI(a, c) end; - 12: begin a := Rnd(3); b := Reg; if a = 0 then a := shShl else if a = 1 then a := shShr else a := shSar; Shift(a, b) end; - 13: begin a := Rnd(3); b := Reg; c := Rnd(64); if a = 0 then a := shShl else if a = 1 then a := shShr else a := shSar; ShiftI(a, b, c) end; - 14: begin a := Rnd(4); b := Reg; if a < 2 then a := a + 2 else a := a + 4; Unary(a, b) end; - 15: Cqo; - 16: begin a := Reg; b := 1 shl Rnd(3); c := Rnd(2); Extend(a, b, c = 0) end; - 17: begin a := Rnd(16); b := Reg; SetCC(a, b) end; - 18: begin a := Reg; b := Reg; TestRR(a, b) end; - 19: begin a := Reg; Push(a) end; - 20: begin a := Reg; Pop(a) end; - 21: begin a := Rnd(5); CallChunk(a) end; - 22: begin a := Rnd(1000); CallBack(a) end; - 23: begin a := Reg; CallR(a) end; - 24: begin a := Reg; JmpR(a) end; - 25: begin m := RandMem; CallM(m) end; - 26: begin a := Rnd(5); case a of 0: Ret; 1: Leave; 2: Syscall; 3: Mfence; else Ud2 end end; - 27: begin a := 8 shl Rnd(3); Align(a) end; - 28: begin a := Rnd(5); case a of 0: RepMovsb; 1: Std; 2: Cld; 3: RepStosb; else RepeCmpsb end end; - 29: begin m := RandMem; a := Reg; LockCmpxchg(m, a) end; - 30: begin m := RandMem; a := Reg; Xchg(m, a) end; - 31: begin m := RandMem; a := Reg; LockXadd(m, a) end; - 32: begin a := Reg; b := Reg; MovqXR(a, b) end; - 33: begin a := Reg; b := Reg; MovqRX(a, b) end; - 34: begin a := Reg; b := Reg; MovX(a, b) end; - 35: begin a := Reg; m := RandMem; b := Rnd(2); MovsXM(a, m, b = 0) end; - 36: begin m := RandMem; a := Reg; b := Rnd(2); MovsMX(m, a, b = 0) end; - 37: begin - d := Rnd(5); a := Reg; b := Reg; c := Rnd(2); - case d of 0: d := $58; 1: d := $59; 2: d := $5C; 3: d := $5E; else d := $5A end; - Sse(d, a, b, c = 0); - end; - 38: begin - d := Rnd(5); a := Reg; m := RandMem; c := Rnd(2); - case d of 0: d := $58; 1: d := $59; 2: d := $5C; 3: d := $5E; else d := $5A end; - SseM(d, a, m, c = 0); - end; - 39: begin a := Reg; b := Reg; c := Rnd(2); Ucomis(a, b, c = 0) end; - 40: begin a := Reg; b := Reg; c := Rnd(2); Cvtsi2s(a, b, c = 0) end; - 41: begin a := Reg; b := Reg; c := Rnd(2); Cvtts2si(a, b, c = 0) end; - 42: begin a := Reg; b := Rnd(64); Btc(a, b) end; - 43: begin - a := Rnd(16); b := Rnd(2); - if b = 0 then Jmp(lab1) else Jcc(a, lab2); - end; - 44: begin a := Rnd(16); b := Rnd(2); c := pc - Rnd(200); if b = 0 then JmpBack(c) else JccBack(a, c) end; - 45: if Rnd(3) = 0 then begin FixLink(lab1); lab1 := NoLabel end - else begin FixLinkTo(lab2, pc - 20); lab2 := NoLabel end; - 46: begin Jmp(lab1); DropJumpTo(lab1) end; - 47: begin a := JoinLinks(lab1, lab2); lab1 := a; lab2 := NoLabel end; - else begin a := Rnd(256); Put1(a) end; - end; - end; - FixLink(lab1); FixLink(lab2); - EndChunk(0); - chunks^[0].live := true; - writeln('pc ', pc, ' fixups ', numFixups, ' lines ', numLines, ' said ', SaidCount); - for i := 0 to pc - 1 do write(HexStr(code^[i], 2)); - writeln; - for i := 0 to numFixups - 1 do writeln('F ', fixups^[i].at, ' ', fixups^[i].chunk, ' ', fixups^[i].addend); - List(-1); -end. diff --git a/src/test/X64Dump.raz b/src/test/X64Dump.raz deleted file mode 100644 index 7494488..0000000 --- a/src/test/X64Dump.raz +++ /dev/null @@ -1,208 +0,0 @@ -(* Writes every instruction the encoder makes, with operands drawn by a - fixed pseudo-random script, then prints the bytes, the fixups and the - listing (raz -S) of what it made: src/X64.raz's side of a test - against the seed's, X64Dump.pas, which prints the same. *) -module X64Dump; -import Out, Fmt, Diag, Scan, Chunk, X64; - -var seed: I64; - -proc rnd(n: INT): INT; -begin - seed := (seed * 1103515245 + 12345) & 7FFFFFFFH; - return INT((seed shr 8) mod I64(n)); -end; - -(* a register *) -proc reg: INT; -begin - return rnd(16); -end; - -(* a size of 1, 2, 4 or 8 bytes *) -proc size: INT; -begin - return 1 shl rnd(4); -end; - -(* a value that fits sz bytes, of 8 as 32 bits *) -proc imm(sz: INT): INT; -var x, r: INT; -begin - x := rnd(1 shl 30); - match rnd(3) of - | 0: r := x mod 100 - 50; - | 1: r := x - (1 shl 29); - | else: r := x mod 20000 - 10000; - end; - if sz = 1 then - r := r mod 128; - elsif sz = 2 then - r := r mod 32768; - end; - return r; -end; - -proc randMem: X64.Mem; -var kind, base, idx, sc, disp, x: INT; -begin - kind := rnd(6); - base := reg(); idx := reg(); sc := 1 shl rnd(4); x := rnd(5); - match x of - | 0: disp := 0; - | 1: disp := rnd(250) - 125; - | 2: disp := rnd(1 shl 20) - (1 shl 19); - | 3: disp := 127 + rnd(3) - 1; - | else: disp := -128 + rnd(3) - 1; - end; - match kind of - | 0, 1: return X64.mem(base, disp); - | 2: return X64.memRip(rnd(5), disp); - | 3, 4: return X64.memIdx(base, idx, sc, disp); - | else: return X64.mem(base, 0); - end; -end; - -proc main*; -var - i, k, op, a, b, c, d, lab1, lab2: INT; m: X64.Mem; path: []U8; -begin - seed := 3; - path := "lib/Rt.raz"; - if LEN(Scan.open(path)) # 0 then Diag.fatal("no file"); end; - X64.setListing(true); - for i := 0 to 4 do c := Chunk.newChunk(Chunk.secText, i - 1, i); end; - Chunk.setCurFile(Scan.intern(ADR(path[0]), LEN(path))); - Chunk.beginChunk(0); - lab1 := X64.NoLabel; lab2 := X64.NoLabel; - for k := 1 to 3000 do - if k mod 7 = 0 then a := rnd(300); b := rnd(60); Chunk.noteLine(1 + a, b); end; - op := rnd(50); - match op of - | 0: a := reg(); b := reg(); X64.movRR(a, b); - | 1: - a := reg(); b := rnd(6); c := rnd(1 shl 30); - match b of - | 0: X64.movRI(a, 0); - | 1: X64.movRI(a, c); - | 2: X64.movRI(a, -c); - | 3: X64.movRI(a, I64(c) * 8 + 5); - | 4: X64.movRI(a, I64(c) * 4096 * 4096 * 9 - 7); - | else: X64.movRI(a, 0); - end; - | 2: a := rnd(8); m := randMem(); b := size(); c := imm(b); X64.aluMI(a, m, c, b); - | 3: m := randMem(); b := size(); c := imm(b); X64.storeI(m, c, b); - | 4: a := reg(); m := randMem(); b := size(); c := rnd(2); X64.load(a, m, b, c = 0); - | 5: m := randMem(); a := reg(); b := size(); X64.store(m, a, b); - | 6: a := reg(); m := randMem(); X64.lea(a, m); - | 7: a := rnd(8); b := reg(); c := reg(); X64.alu(a, b, c); - | 8: a := rnd(8); b := reg(); c := imm(8); X64.aluI(a, b, c); - | 9: a := rnd(8); b := reg(); m := randMem(); X64.aluM(a, b, m); - | 10: a := reg(); b := reg(); X64.imul(a, b); - | 11: a := reg(); c := imm(8); X64.imulI(a, c); - | 12: - a := rnd(3); b := reg(); - if a = 0 then a := X64.shShl; elsif a = 1 then a := X64.shShr; else a := X64.shSar; end; - X64.shift(a, b); - | 13: - a := rnd(3); b := reg(); c := rnd(64); - if a = 0 then a := X64.shShl; elsif a = 1 then a := X64.shShr; else a := X64.shSar; end; - X64.shiftI(a, b, c); - | 14: - a := rnd(4); b := reg(); - if a < 2 then a := a + 2; else a := a + 4; end; - X64.unary(a, b); - | 15: X64.cqo; - | 16: a := reg(); b := 1 shl rnd(3); c := rnd(2); X64.extend(a, b, c = 0); - | 17: a := rnd(16); b := reg(); X64.setCC(a, b); - | 18: a := reg(); b := reg(); X64.testRR(a, b); - | 19: a := reg(); X64.push(a); - | 20: a := reg(); X64.pop(a); - | 21: a := rnd(5); X64.callChunk(a); - | 22: a := rnd(1000); X64.callBack(a); - | 23: a := reg(); X64.callR(a); - | 24: a := reg(); X64.jmpR(a); - | 25: m := randMem(); X64.callM(m); - | 26: - a := rnd(5); - match a of - | 0: X64.ret; - | 1: X64.leave; - | 2: X64.syscall; - | 3: X64.mfence; - | else: X64.ud2; - end; - | 27: a := 8 shl rnd(3); X64.align(a); - | 28: - a := rnd(5); - match a of - | 0: X64.repMovsb; - | 1: X64.std; - | 2: X64.cld; - | 3: X64.repStosb; - | else: X64.repeCmpsb; - end; - | 29: m := randMem(); a := reg(); X64.lockCmpxchg(m, a); - | 30: m := randMem(); a := reg(); X64.xchg(m, a); - | 31: m := randMem(); a := reg(); X64.lockXadd(m, a); - | 32: a := reg(); b := reg(); X64.movqXR(a, b); - | 33: a := reg(); b := reg(); X64.movqRX(a, b); - | 34: a := reg(); b := reg(); X64.movX(a, b); - | 35: a := reg(); m := randMem(); b := rnd(2); X64.movsXM(a, m, b = 0); - | 36: m := randMem(); a := reg(); b := rnd(2); X64.movsMX(m, a, b = 0); - | 37: - d := rnd(5); a := reg(); b := reg(); c := rnd(2); - match d of - | 0: d := 58H; - | 1: d := 59H; - | 2: d := 5CH; - | 3: d := 5EH; - | else: d := 5AH; - end; - X64.sse(d, a, b, c = 0); - | 38: - d := rnd(5); a := reg(); m := randMem(); c := rnd(2); - match d of - | 0: d := 58H; - | 1: d := 59H; - | 2: d := 5CH; - | 3: d := 5EH; - | else: d := 5AH; - end; - X64.sseM(d, a, m, c = 0); - | 39: a := reg(); b := reg(); c := rnd(2); X64.ucomis(a, b, c = 0); - | 40: a := reg(); b := reg(); c := rnd(2); X64.cvtsi2s(a, b, c = 0); - | 41: a := reg(); b := reg(); c := rnd(2); X64.cvtts2si(a, b, c = 0); - | 42: a := reg(); b := rnd(64); X64.btc(a, b); - | 43: - a := rnd(16); b := rnd(2); - if b = 0 then X64.jmp(lab1); else X64.jcc(a, lab2); end; - | 44: - a := rnd(16); b := rnd(2); c := Chunk.pc - rnd(200); - if b = 0 then X64.jmpBack(c); else X64.jccBack(a, c); end; - | 45: - a := rnd(3); - if a = 0 then - X64.fixLink(lab1); lab1 := X64.NoLabel; - else - X64.fixLinkTo(lab2, Chunk.pc - 20); lab2 := X64.NoLabel; - end; - | 46: X64.jmp(lab1); X64.dropJumpTo(lab1); - | 47: a := X64.joinLinks(lab1, lab2); lab1 := a; lab2 := X64.NoLabel; - | else: a := rnd(256); Chunk.put1(a); - end; - end; - X64.fixLink(lab1); X64.fixLink(lab2); - Chunk.endChunk(0); - Chunk.setLive(0); - Fmt.print("pc {} fixups {} lines {} said {}\n", [Fmt.int(Chunk.pc), Fmt.int(Chunk.numFixups), - Fmt.int(Chunk.numLines), Fmt.int(X64.saidCount())]); - for i := 0 to Chunk.pc - 1 do Out.hex(U64(Chunk.text[i]), 2); end; - Out.ln; - for i := 0 to Chunk.numFixups - 1 do - Fmt.print("F {} {} {}\n", [Fmt.int(Chunk.fixups[i].at), Fmt.int(Chunk.fixups[i].chunk), Fmt.int(Chunk.fixups[i].addend)]); - end; - X64.list(-1); -end; - -end X64Dump. diff --git a/src/test/genasm.py b/src/test/genasm.py deleted file mode 100644 index 5749c55..0000000 --- a/src/test/genasm.py +++ /dev/null @@ -1,244 +0,0 @@ -#!/usr/bin/env python3 -"""Writes the files the assembler of X64 is tested on, into the directory -given: valid.raz, a long run of instructions of every kind the assembler -knows, and one small file for each error it reports. Each ends in `end`, -as a code procedure's body does. Fixed seed: the same files each run.""" -import random, sys, os - -random.seed(7) -out = sys.argv[1] -os.makedirs(out, exist_ok=True) - -R64 = "rax rcx rdx rbx rsp rbp rsi rdi r8 r9 r10 r11 r12 r13 r14 r15".split() -R32 = "eax ecx edx ebx esp ebp esi edi r8d r9d r10d r11d r12d r13d r14d r15d".split() -R16 = "ax cx dx bx sp bp si di r8w r9w r10w r11w r12w r13w r14w r15w".split() -R8 = "al cl dl bl spl bpl sil dil r8b r9b r10b r11b r12b r13b r14b r15b".split() -BY = {8: R64, 4: R32, 2: R16, 1: R8} -PRE = {8: "qword ", 4: "dword ", 2: "word ", 1: "byte "} -XM = ["xmm%d" % i for i in range(16)] -CC = "o no b ae e ne be a s ns p np l ge le g c nae nc nb z nz na nbe nge nl ng nle".split() - - -def reg(size=8): - return random.choice(BY[size]) - - -def mem(size=0): - k = random.randrange(8) - b = random.choice(R64) - i = random.choice([r for r in R64 if r != "rsp"]) - d = random.choice([0, 8, -8, 127, -128, 128, 4096, -4096, 100000]) - s = random.choice([1, 2, 4, 8]) - p = PRE[size] if size else "" - if k == 0: - t = "[%s]" % b - elif k == 1: - t = "[%s + %d]" % (b, abs(d)) if d >= 0 else "[%s - %d]" % (b, -d) - elif k == 2: - t = "[%s + %s * %d]" % (b, i, s) - elif k == 3: - t = "[%s + %s * %d + %d]" % (b, i, s, abs(d)) if d >= 0 else "[%s + %s * %d - %d]" % (b, i, s, -d) - elif k == 4: - t = "[%s + %s]" % (b, i) - elif k == 5: - t = "[%s + 0]" % b - elif k == 6: - t = "[rsp + %d]" % abs(d) - else: - t = "[%s - %d]" % (b, abs(d) + 1) - return p + t - - -def imm(size): - if size == 1: - return str(random.choice([0, 1, -1, 127, 255, -128, 77])) - if size == 2: - return str(random.choice([0, 1, -1, 300, 32767, -32768, 255, 65535])) - return str(random.choice([0, 1, -1, 127, 128, -128, -129, 100000, -100000, 2147483647, -2147483648, 4294967295])) - - -def size(): - return random.choice([1, 2, 4, 8]) - - -lines = [] - - -def emit(s): - lines.append(" " + s + ";") - - -for n in range(900): - k = random.randrange(40) - sz = size() - if k < 6: - op = random.choice("add or adc sbb and sub xor cmp".split()) - f = random.randrange(5) - if f == 0: - emit("%s %s, %s" % (op, reg(sz), reg(sz))) - elif f == 1: - emit("%s %s, %s" % (op, reg(sz), mem(sz if random.random() < .5 else 0))) - elif f == 2: - emit("%s %s, %s" % (op, mem(sz if random.random() < .5 else 0), reg(sz))) - elif f == 3: - emit("%s %s, %s" % (op, reg(sz), imm(sz))) - else: - emit("%s %s, %s" % (op, mem(sz), imm(sz))) - elif k < 10: - f = random.randrange(5) - if f == 0: - emit("mov %s, %s" % (reg(sz), reg(sz))) - elif f == 1: - emit("mov %s, %s" % (reg(sz), mem())) - elif f == 2: - emit("mov %s, %s" % (mem(), reg(sz))) - elif f == 3: - v = random.choice([0, 5, -5, 2147483647, 4294967295, 4294967296, -2147483649, 123456789012345]) - emit("mov %s, %d" % (reg(8 if sz >= 4 else sz) if sz >= 4 else reg(sz), v if sz == 8 else (v % 65536 if sz == 2 else v % 256 if sz == 1 else v % 4294967296))) - else: - emit("mov %s, %s" % (mem(sz), imm(sz))) - elif k == 10: - emit("test %s, %s" % (reg(sz), random.choice([reg(sz), imm(sz) if sz != 1 else "1"]))) - elif k == 11: - emit("xchg %s, %s" % (random.choice([reg(sz), mem()]), reg(sz))) - elif k == 12: - emit("%slock %s %s, %s" % ("", random.choice(["cmpxchg", "xadd"]), mem(), reg(sz))) - elif k == 13: - emit("lea %s, %s" % (reg(random.choice([2, 4, 8])), mem())) - elif k == 14: - op = random.choice(["movzx", "movsx"]) - emit("%s %s, %s" % (op, reg(random.choice([2, 4, 8])), random.choice([mem(random.choice([1, 2])), reg(random.choice([1, 2]))]))) - elif k == 15: - emit("movsxd %s, %s" % (reg(8), random.choice([mem(4), reg(4)]))) - elif k == 16: - emit("%s %s" % (random.choice("not neg mul imul div idiv inc dec".split()), random.choice([reg(sz), mem(sz)]))) - elif k == 17: - emit("imul %s" % ", ".join([reg(sz if sz > 1 else 4), random.choice([reg(sz if sz > 1 else 4)] * 1 + [mem()])] + ([imm(sz if sz > 1 else 4)] if random.random() < .4 else [])).replace("%", "%")) - elif k == 18: - op = random.choice("rol ror rcl rcr shl shr sal sar".split()) - emit("%s %s, %s" % (op, random.choice([reg(sz), mem(sz)]), random.choice([str(random.randrange(64)), "cl"]))) - elif k == 19: - emit("%s %s" % (random.choice(["push", "pop"]), reg(8))) - elif k == 20: - op = random.choice(["call", "jmp"]) - emit("%s %s" % (op, random.choice([reg(8), mem()]) if op == "call" else mem())) - elif k == 21: - emit("set%s %s" % (random.choice(CC), random.choice([reg(1), mem(1)]))) - elif k == 22: - emit("cmov%s %s, %s" % (random.choice(CC), reg(random.choice([2, 4, 8])), random.choice([reg(8), mem()]))) - elif k == 23: - op = random.choice("sqrt add mul sub min div max".split()) + random.choice(["sd", "ss"]) - emit("%s %s, %s" % (op, random.choice(XM), random.choice([random.choice(XM), mem()]))) - elif k == 24: - op = random.choice("punpcklbw punpcklwd pcmpeqb pand pandn por pxor paddb psubb".split()) - emit("%s %s, %s" % (op, random.choice(XM), random.choice([random.choice(XM), mem()]))) - elif k == 25: - emit("pshufd %s, %s, %d" % (random.choice(XM), random.choice([random.choice(XM), mem()]), random.randrange(256))) - elif k == 26: - op = random.choice(["movdqu", "movdqa"]) - if random.random() < .5: - emit("%s %s, %s" % (op, random.choice(XM), random.choice([random.choice(XM), mem()]))) - else: - emit("%s %s, %s" % (op, mem(), random.choice(XM))) - elif k == 27: - emit("pmovmskb %s, %s" % (reg(4), random.choice(XM))) - elif k == 28: - emit("%s %s, %s" % (random.choice("bsf bsr tzcnt lzcnt popcnt".split()), reg(random.choice([4, 8])), random.choice([reg(8), mem()]))) - elif k == 29: - op = random.choice(["movsd", "movss"]) - if random.random() < .5: - emit("%s %s, %s" % (op, random.choice(XM), random.choice([random.choice(XM), mem()]))) - else: - emit("%s %s, %s" % (op, mem(), random.choice(XM))) - elif k == 30: - op = random.choice(["movq", "movd"]) - if random.random() < .5: - emit("%s %s, %s" % (op, random.choice(XM), random.choice([reg(8), mem()]))) - else: - emit("%s %s, %s" % (op, random.choice([reg(8), mem()]), random.choice(XM))) - elif k == 31: - emit("%s %s, %s" % (random.choice(["ucomisd", "ucomiss"]), random.choice(XM), random.choice([random.choice(XM), mem()]))) - elif k == 32: - emit("%s %s, %s" % (random.choice(["cvtsi2sd", "cvtsi2ss"]), random.choice(XM), random.choice([reg(random.choice([4, 8])), mem(random.choice([4, 8]))]))) - elif k == 33: - emit("%s %s, %s" % (random.choice(["cvttsd2si", "cvttss2si"]), reg(random.choice([4, 8])), random.choice([random.choice(XM), mem()]))) - elif k == 34: - emit("%s %s, %s" % (random.choice(["cvtsd2ss", "cvtss2sd"]), random.choice(XM), random.choice([random.choice(XM), mem()]))) - elif k == 35: - emit(random.choice("ret leave syscall cpuid rdtsc pause nop ud2 int3 hlt cqo cdq mfence lfence sfence".split())) - elif k == 36: - emit("db " + ", ".join(str(random.choice([0, 1, 255, 128, 77, -1, -128])) for _ in range(random.randrange(1, 6)))) - elif k == 37: - lines.append(" L%d:" % n) - lines.append(" add rax, 1;") - lines.append(" j%s L%d;" % (random.choice(CC), n)) - lines.append(" jmp L%d;" % n) - elif k == 38: - lines.append(" jmp F%d;" % n) - lines.append(" nop;") - lines.append(" j%s F%d;" % (random.choice(CC), n)) - lines.append(" F%d:" % n) - else: - emit("lock add %s, %s" % (mem(sz), reg(sz))) - -open(os.path.join(out, "valid.raz"), "w").write("\n".join(lines) + "\n end\n") - -errors = { - "empty": "", - "badop": "foo rax;", - "noinsn": "42;", - "nosemi": "nop", - "undefined": "jmp nowhere;", - "twice": "a: nop; a: nop;", - "toomany": "add rax, rbx, rcx, rdx;", - "movimm": "mov 1, 2;", - "movmm": "mov [rax], [rbx];", - "addimm": "add 1, rax;", - "pushmem": "push [rax];", - "pushsmall": "push eax;", - "badmem": "mov rax, [rsi * 4];", - "badscale": "mov rax, [rsi + rdi * 3];", - "nomem": "mov rax, [];", - "unclosed": "mov rax, [rbx;", - "shiftcl": "shl rax, dl;", - "shiftimm": "shl 3, 1;", - "lea": "lea rax, rbx;", - "movzx": "movzx rax, rbx;", - "movzx4": "movzx rax, dword [rbx];", - "setsize": "sete rax;", - "cmovimm": "cmove rax, 5;", - "sse": "addsd rax, xmm1;", - "sse1": "addsd xmm1;", - "pshufd": "pshufd xmm1, xmm2;", - "pand3": "pand xmm1, xmm2, 3;", - "movdq": "movdqu rax, xmm1;", - "pmov": "pmovmskb xmm1, rax;", - "bits": "popcnt ax, bx;", - "movsd": "movsd rax, xmm1;", - "movq": "movq rax, rbx;", - "ucomi": "ucomisd rax, xmm1;", - "cvtsi": "cvtsi2sd rax, xmm1;", - "cvtt": "cvttsd2si xmm1, xmm2;", - "cvtss": "cvtss2sd rax, xmm1;", - "plainops": "ret rax;", - "dbrange": "db 256;", - "dbneg": "db -129;", - "imul1": "imul rax, 5;", - "imulmem": "imul [rax], rbx;", - "unary2": "not rax, rbx;", - "unaryimm": "neg 5;", - "inc": "inc 5;", - "xchgimm": "xchg rax, 5;", - "cmpxchg": "cmpxchg rax, 5;", - "calli": "call 5;", - "jmpimm": "jmp 5;", - "labels65": "".join("l%d: nop; " % i for i in range(65)), - "xmm16": "addsd xmm16, xmm1;", - "noend": None, -} -for name, body in errors.items(): - if body is None: - text = " nop;\n" - else: - text = " " + body + "\n end\n" - open(os.path.join(out, "err_%s.raz" % name), "w").write(text) diff --git a/src/test/genlex.py b/src/test/genlex.py deleted file mode 100644 index f2607ea..0000000 --- a/src/test/genlex.py +++ /dev/null @@ -1,29 +0,0 @@ -# Writes into the directory given a file for each message the scanner -# makes, with the places diag shows them in: after a tab, in a long -# line, after characters of several bytes, in a CRLF file. -import sys -d = sys.argv[1] -cases = { - "slip1": "x == y;\n", "slip2": "a && b;\n", "slip3": "a || b;\n", "slip4": "a != b;\n", "slip5": "!a;\n", - "str": "x := \"abc\n", "chr": "x := 'a\n", "chr2": "x := 'ab';\n", "chr3": "x := '';\n", - "esc": "x := \"\\q\";\n", "esc2": "x := 'a\\x41';\n", "esc3": "x := \"\\x4\";\n", - "esc4": "x := \"\\u41\";\n", "esc5": "x := \"\\u{110000}\";\n", "esc6": "x := \"\\u{D800}\";\n", - "esc7": "x := \"\\u{1234567}\";\n", "esc8": "x := \"\\u{}\";\n", "esc9": "x := \"\\u{41\";\n", - "esc10": "x := \"\\u{1F600}\\u{e9}\\x00\\xff\\n\\t\\r\\0\\\\\\'\\\"\";\n", - "cmt": "x := 1; (* open (* nested *)\n", "nul": "x := 1;\0 y;\n", "hexbad": "x := 12AB;\n", - "hexbig": "x := 1FFFFFFFFFFFFFFFFH;\n", "hexok": "x := 0FFFFFFFFFFFFFFFFH; y := 7FFFFFFFFFFFFFFFH;\n", - "decbig": "x := 9223372036854775808;\n", "decmax": "x := 9223372036854775807;\n", - "realhex": "x := 1A.5;\n", "realexp": "x := 1.5E;\n", "realexp2": "x := 1.5E+;\n", "realbig": "x := 1.0E309;\n", - "realmax": "x := 1.7976931348623159E308;\n", "reallong": "x := " + "1" * 254 + ".5;\n", - "digname": "x := 12abc;\n", "uni": "x := \u00e9;\n", "badutf": b"x := \"a\xc3(\";\n", "badutf2": b"x := \"\xed\xa0\x80\";\n", - "badutf3": b"x := \"\xc0\x80\";\n", "badutf4": b"x := \"\xf5\x80\x80\x80\";\n", "badutf5": b"x := \"\xe2\x82\";\n", - "okutf": "x := \"h\u00e9llo \u20ac \U0001F600\"; y := '\u00e9'; (* \u00e9 *)\n", - "unexp": "x := @;\n", "long": "x := " + "a + " * 60 + "@;\n", "longmid": "x := " + "a + " * 40 + "@ " + "+ b" * 40 + ";\n", - "tab": "\t\tx := @;\n", "multi": "x := \"\u00e9\u00e9\u00e9\" + @;\n", "ident64": "x" * 64 + ";\n", "ident63": "x" * 63 + ";\n", - "bigstr": "x := \"" + "a" * 70000 + "\";\n", "empty": "", "nonl": "x := y", - "ops": "+ - * / = # < <= > >= := : ; , . .. ... ( ) [ ] { } ^ & ~ | _ __ _a\n", - "words": "and begin break by code const defer div do else elsif end extern for forward if import in link match mod module not of or packed proc record repeat return set shl shr then to try type until var variant VOLATILE while xor Volatile\n", -} -for k, v in cases.items(): - open(f"{d}/{k}.raz", "wb").write(v.encode("utf-8", "surrogateescape") if isinstance(v, str) else v) -open(f"{d}/crlf.raz", "wb").write(b"x := 1;\r\ny := @;\r\n") diff --git a/src/test/genreals.py b/src/test/genreals.py deleted file mode 100644 index 3074392..0000000 --- a/src/test/genreals.py +++ /dev/null @@ -1,19 +0,0 @@ -# Prints a raz module of reals that are hard to read exactly: the -# extremes, halves between neighbours, and many random ones. -import random -random.seed(7) -v = ["0.0", "1.0", "0.1", "4.9E-324", "2.4703282292062327E-324", "2.4703282292062328E-324", - "2.2250738585072014E-308", "2.2250738585072011E-308", "1.7976931348623157E308", - "9007199254740993.0", "9007199254740992.5", "9007199254740993.5", "0.000001E+3", - "123456789012345678901234567890.0E-10", "1.0E-400", "0.5E1", "5.0E-1"] -for _ in range(3000): - d = random.randint(1, 10 ** random.randint(1, 30)) - e = random.randint(-320, 300) - s = str(d) - v.append(s[0] + "." + (s[1:] or "0") + "E" + ("%+d" % e if random.random() < .5 else str(e))) -for _ in range(500): - v.append("%d.%d" % (random.randint(0, 10 ** 6), random.randint(0, 10 ** 12))) -print("module Reals;\nvar x: F64;\nbegin") -for s in v: - print(" x := " + s + ";") -print("end Reals.") diff --git a/src/test/seedcontinue.pas b/src/test/seedcontinue.pas deleted file mode 100644 index 5b09349..0000000 --- a/src/test/seedcontinue.pas +++ /dev/null @@ -1,20 +0,0 @@ -{ The seed has no continue, which report 2026-10.31 made a reserved - word, the last of them in src/Scan.raz. This unit interns the name - right after the seed's reserved words, before any other, so that - every name after it has the number src/ gives it: each dumper that is - compared with src/'s side uses it straight after scan. } -unit seedcontinue; - -{$mode objfpc}{$H-} - -interface - -implementation - -uses scan; - -const Word: string = 'continue'; - -initialization - Intern(@Word[1], length(Word)); -end. diff --git a/test/check.sh b/test/check.sh index e4a2ca2..2aeffa7 100755 --- a/test/check.sh +++ b/test/check.sh @@ -252,6 +252,17 @@ if (ulimit -v 60000; $out/lib_Thread.linux-x86_64 fail) > /dev/null 2>&1; then pass=$((pass + 1)) else echo "FAIL Thread under a memory limit"; fail=$((fail + 1)); fi +# the compiler's way of going on after an error, in small: a parser +# that reads names, fails on each bad one, and goes on to the next +if $raz -Werror -I src -o $out/recover src/test/Recover.raz 2>$out/recover.cerr; then + printf 'a bad b bad bad c\nd bad\n' >$out/rec.raz + r=$($out/recover $out/rec.raz 2>/dev/null) || true + if [ "$r" = "8 names, 4 failed, 4 errors" ]; then pass=$((pass + 1)) + else echo "FAIL src/test/Recover.raz: $r"; fail=$((fail + 1)); fi +else + echo "FAIL src/test/Recover.raz: $(cat $out/recover.cerr)"; fail=$((fail + 1)) +fi + # a module imported and never called leaves nothing of its own in the # program, not even Width's tables: the same program, importing it or # not, makes a file of the same size (the names the trace shows may