larsbrinkhoff / lbForth

Self-hosting metacompiled Forth, bootstrapping from a few lines of C; targets Linux, Windows, ARM, RISC-V, 68000, PDP-11, asm.js.
GNU General Public License v3.0
414 stars 113 forks source link

Undefined: TO_DOES #86

Closed Phi-Ho closed 3 years ago

Phi-Ho commented 3 years ago

Greetings,

Please advise how can this issue be fixed. Please find related info appended below. ctc.fth was used instead of missing 'threading.fth' in src/dictionary.fth There were some modifications in the forth files to resolve the path of the included files.

Regards,

phiho

lbForth ok include targets/x86/build.fth

(Debug print include targets/x86/nucleus.fth include src/kernel.fth include src/dictionary.fth include targets/x86/jump.fth include targets/ctc.fth ) Undefined: TO_DOES

ok ' TO_DOES Undefined: TO_DOES

$ fgrep -r TO_DOES lisp/meta.lisp:;; UNTIL CELL NAME_LENGTH TO_NEXT TO_CODE TO_DOES TO_BODY lisp/meta.lisp: "TO_CODE" "TO_DOES" "TO_BODY" "[" "[DEFINED]" "[UNDEFINED]" src/compile.fth:has-does? [if] does-offset t-constant TO_DOES [then] targets/c/meta.fth: CELL NAME_LENGTH TO_NEXT TO_CODE TO_DOES TO_BODY targets/ctc.fth:: >does ( a -- a' ) TO_DOES + ;
targets/x16/meta.fth:h: TO_DOES 0 t-literal ; $

larsbrinkhoff commented 3 years ago

TO_DOES is defined in targets/ctc.fth, but it's only for use with the "c" target. Really, you should run make using TARGET and possibly OS as inputs. The appropriate Makefile target will copy the necessary files to the expected locations. This happens in the target-specific forth.mk file.

For example make all TARGET=x86 OS=linux will run these commands:

make -ftargets/c/bootstrap.mk
make[1]: Entering directory '/home/lars/src/lbForth'
make[1]: Circular kernel.c <- b-forth dependency dropped.
gcc -m32 -O2 -fomit-frame-pointer -fno-unit-at-a-time -Itargets/c targets/c/params.c -o params
targets/c/run.sh ./params -lisp > params.lisp
cp targets/c/jump.fth jump.fth
cp targets/ctc.fth threading.fth
cp targets/c/target.fth target.fth
echo ": sysdir   s\" src/\" ;" >> target.fth
./lisp/lisp.sh '(load "lisp/meta.lisp") (compile-forth "targets/c/nucleus.fth" "src/kernel.fth")'
gcc -m32 -O2 -fomit-frame-pointer -fno-unit-at-a-time -Itargets/c  -c -o kernel.o kernel.c
gcc -m32 kernel.o -o b-forth
rm -f kernel.c kernel.o params.lisp jump.fth threading.fth target.fth
make[1]: Leaving directory '/home/lars/src/lbForth'
cp b-forth forth

And so on. Note the three cp commands which provide jump.fth, threading.fth, and target.fth for the particular target.

Phi-Ho commented 3 years ago

Hi,

Thank you very much for the advice.

Now this command:

$ echo "include targets/x86/build.fth" | ./forth

completes without errors. Does that mean the meta build was successful? (There seems to be lot of binary flying across the screen.)

According to "build.md":

- Finally, forward references are resolved, and the image is saved to disk.

I couldn't find anything new on the root directory of lbForth.

Please advise where is the image saved on the disk.

Regards,

phiho

larsbrinkhoff commented 3 years ago

Yes, that command is the meta build. The output you see is the finished image which is supposed to be redirected to a file:

echo "include targets/x86/build.fth" | ./forth > image

(For quirky bootstrapping reasons, the image may be output like this, or it may be written to a file.)

Phi-Ho commented 3 years ago

Hi,

After commenting out the bottom part of "compile.fth":

0 [if]
cr .( Target size: ) t-size . cr .( Target used: ) target here host also meta >host t-image host - . cr .( Host unused: ) unused . cr .( Target words: ) also t-words words only forth cr .( Forward refs: ) also meta also forward-refs words cr target-region hex dump bye
[then]

"4th.exe" still can not run under Cygwin:

$ ./4th ./4th: line 1: lbForth: command not found ./4th: line 2: ok: command not found ./4th: line 3: syntax error near unexpected token (' ./4th: line 3:MZ@PEL▒ T:@▒t▒.code▒dh* ▒ExitProcessGetStdHandleCreateFileA0ReadFileHWriteFile\CloseHandlepkernel32.dll▒▒(▒@▒T▒h▒▒get-std-handle@@▒▒▒(@P▒Ë▒▒▒"emit <@d@▒▒Pj▒▒(@jh$@jRP▒h@X▒▒▒▒▒▒"bye `@▒@j▒@ open-file▒@▒@▒▒PV▒s ▒=▒@▒▒jh▒▒Pjj▒CPh▒@▒@@3▒▒▒u▒▒▒C ^X▒▒▒▒▒" read-file▒@@▒▒Pjh$@▒CP▒P▒P▒T@3҅▒u▒▒▒▒▒▒S▒$@▒X▒▒▒▒▒▒"' $

and WSL

$ ./4th ./4th: line 1: lbForth: command not found ./4th: line 2: ok: command not found ./4th: line 3: syntax error near unexpected token (' ./4th: line 3:ELFԨ44 ((hcemit dPSQʺ̀Y[X"bye `̀ open-filePSQVt$=L$^L$ ̀3ۅy؉\$D$Y[X" read-filePSQ\$ L$T$̀3ۅy؉\$D$Y[XZ"' $

Regards,

phiho

larsbrinkhoff commented 3 years ago

There may be some tricks to get the image file right. You should use make TARGET=c to create it rather than trying to do it manually.

Phi-Ho commented 3 years ago

Hi,

Do you think it would be simpler to implement write-file?

Regards, phiho

larsbrinkhoff commented 3 years ago

The "c" target is intended to be a bare minimum implementation, which is why it doesn't have write-file. It's only used to bootstrap the other, more featureful targets.

If you use the provided makefiles, there shouldn't be a problem to build the "c" target. If you do it manually, there may be some gotchas.

Phi-Ho commented 3 years ago

Hi,

Is the following information worth while?

4.exe was generated from 4th.exe with tail:

$ tail -20 4th.exe > 4.exe $ wc 4th.exe 21 216 11379 4th.exe $ wc 4.exe 19 214 11368 4.exe

Does this make sense? 4.exe has 2 lines, 2 words and 11 bytes (counting 2 newlines and 9 characters) less than 4th.exe:

lbForth ok

When 4.exe was run, it displayed "lbFortth", that's a good sign, but there was problem ( undefined: docol, ) :

$ ./4 lbForth Undefined: docol,

Any ideas?

Regards,

phiho

larsbrinkhoff commented 3 years ago

The banner message "lbForth" indicates that the word warm in kernel.fth was run. It loads load.fth, which in turn loads core.fth. This last file references docol,, which apparently was not defined. It should be defined in target.fth, which should be built into the kernel i.e. "4.exe". I dont know why this would happen, but if you run make clean all TARGET=c it should be ok.

Phi-Ho commented 3 years ago

I am doing everything afresh now (and got "Undefined: NAME_LENGTH") :

$ make clean all TARGET=c rm -f .o kernel.c params params.fth jump.fth threading.fth target.fth rm -f forth -forth test- -stamp .exe conf.mk forth.html forth.js make -ftargets/c/bootstrap.mk make[1]: Entering directory '/cygdrive/f/fun/lbforth/lbForth-master' make[1]: Circular kernel.c <- b-forth dependency dropped. gcc -m32 -O2 -fomit-frame-pointer -fno-unit-at-a-time -Itargets/c targets/c/params.c -o params targets/c/run.sh ./params -lisp > params.lisp cp targets/c/jump.fth jump.fth cp targets/ctc.fth threading.fth cp targets/c/target.fth target.fth echo ": sysdir s\" src/\" ;" >> target.fth ./lisp/lisp.sh '(load "lisp/meta.lisp") (compile-forth "targets/c/nucleus.fth" "src/kernel.fth")' ;; Loading file lisp/meta.lisp ... ;; Loading file params.lisp ... ;; Loaded file params.lisp ;; Loading file lisp/words.lisp ... ;; Loaded file lisp/words.lisp ;; Loaded file lisp/meta.lisp gcc -m32 -O2 -fomit-frame-pointer -fno-unit-at-a-time -Itargets/c -c -o kernel.o kernel.c gcc -m32 kernel.o -o b-forth rm -f kernel.c kernel.o params.lisp jump.fth threading.fth target.fth make[1]: Leaving directory '/cygdrive/f/fun/lbforth/lbForth-master' cp b-forth forth rm -f -stamp touch c-.-stamp targets/c/run.sh ./params -forth > params.fth cp targets/c/jump.fth jump.fth cp targets/ctc.fth threading.fth cp targets/c/target.fth target.fth echo ": sysdir s\" src/\" ;" >> target.fth echo 'include targets/c/build.fth bye' | targets/c/run.sh ./forth | tail -n+3 > kernel.c ; grep -a Meta-OK kernel.c / Meta-OK */ gcc -m32 -O2 -fomit-frame-pointer -fno-unit-at-a-time -Itargets/c -c -o kernel.o kernel.c gcc -m32 kernel.o -o c-forth rm -f forth.exe cp c-forth forth

$ ls -l *.exe -rwxrwxr-x+ 1 me None 175874 Nov 13 09:10 b-forth.exe -rwxrwxr-x+ 1 me None 175858 Nov 13 09:10 c-forth.exe -rwxrwxr-x+ 1 me None 175858 Nov 13 09:10 forth.exe -rwxrwxr-x+ 1 me None 155564 Nov 13 09:10 params.exe

$ echo "include targets/x86/build.fth" | ./forth lbForth ok Undefined: NAME_LENGTH

$ grep -r NAME_LENGTH | more lisp/meta.lisp:;; UNTIL CELL NAME_LENGTH TO_NEXT TO_CODE TO_DOES TO_BODY lisp/meta.lisp: "REPEAT" "UNTIL" "[CHAR]" "[']" "CELL" "NAME_LENGTH" "TO_NEXT" target.fth:: #name ( -- u ) NAME_LENGTH 1 - ; targets/asmjs/target.fth:: NAME_LENGTH 16 ; targets/asmjs/target.fth:: #name ( -- u ) NAME_LENGTH 1 - ; targets/c/forth.h:#define NAME_LENGTH 16 targets/c/forth.h: char_t name[NAME_LENGTH - 1]; targets/c/meta.fth: CELL NAME_LENGTH TO_NEXT TO_CODE TO_DOES TO_BODY targets/c/meta.fth: CELL NAME_LENGTH targets/c/params.c: output ("name-size", NAME_LENGTH); targets/c/target.fth:: #name ( -- u ) NAME_LENGTH 1 - ; targets/x16/meta.fth:h: NAME_LENGTH 0 t-literal ;

larsbrinkhoff commented 3 years ago

That's because you are trying to run the x86 version of build.fth, but the previous run of make has set up things to build the "c" target.

To build the x86 target, run make clean all TARGET=x86 OS=linux, or make clean all TARGET=x86 OS=windows.

Phi-Ho commented 3 years ago

$ make clean all TARGET=x86 OS=windows targets/x86/forth.mk:1: *** missing separator. Stop.

Fixed by adding "-include": -include ../forth.mk

Once more:

$ make clean all TARGET=x86 OS=windows make: *** No rule to make target 't-clean', needed by 'clean'. Stop.

larsbrinkhoff commented 3 years ago

I'm not sure, but that could be related to CR LF line endings in files.

Here's what it looks like when I run on Linux:

$make clean all TARGET=x86 OS=windows
rm -f params.fth jump.fth threading.fth target.fth
rm -f forth *-forth test-* *-stamp *.exe conf.mk forth.html forth.js
make -ftargets/c/bootstrap.mk
make[1]: Entering directory '/home/lars/src/lbForth'
make[1]: Circular kernel.c <- b-forth dependency dropped.
targets/c/run.sh ./params -lisp > params.lisp
cp targets/c/jump.fth jump.fth
cp targets/ctc.fth threading.fth
cp targets/c/target.fth target.fth
echo ": sysdir   s\" src/\" ;" >> target.fth
./lisp/lisp.sh '(load "lisp/meta.lisp") (compile-forth "targets/c/nucleus.fth" "src/kernel.fth")'
gcc -m32 -O2 -fomit-frame-pointer -fno-unit-at-a-time -Itargets/c  -c -o kernel.o kernel.c
gcc -m32 kernel.o -o b-forth
rm -f kernel.c kernel.o params.lisp jump.fth threading.fth target.fth
make[1]: Leaving directory '/home/lars/src/lbForth'
cp b-forth forth
rm -f *-stamp
touch x86-windows-stamp
cat targets/x86/params.fth targets/x86/windows/params.fth > params.fth
cp targets/x86/jump.fth jump.fth
cp targets/itc.fth threading.fth
cat targets/x86/target.fth targets/x86/windows/target.fth > target.fth
echo ": sysdir   s\" /usr/local/share/lbForth/\" ;" >> target.fth
echo include targets/x86/build.fth | targets/c/run.sh ./forth | tail -n+3 > x86-forth
mv image x86-forth
mv: cannot stat 'image': No such file or directory
targets/x86/forth.mk:5: recipe for target 'x86-forth' failed
make: [x86-forth] Error 1 (ignored)
chmod a+x x86-forth
rm -f forth.exe
cp x86-forth forth
$file forth
forth: MS-DOS executable
$

Note that recipe for target 'x86-forth' failed is expected and the error is ignored.

Phi-Ho commented 3 years ago

Hi,

Just to be sure, did you run two 'make clean all' consecutively:

make clean all TARGET=c make clean all TARGET=x86 OS=windows

or just the second one:

make clean all TARGET=x86 OS=windows

from afresh

Cheers,

phiho

larsbrinkhoff commented 3 years ago

I did make two runs, but it should not matter. The "clean" target should remove all build artifacts and start from scratch.

Phi-Ho commented 3 years ago

It looks like there is an issue with make on my system. I made a modification in makefile:

include targets/forth.mk

instead of

include $(TDIR)/forth.mk

because $(TDIR)/forth.mk has just:

../forth.mk

and now

$ ls -l 4.exe -rwxrwxr-x+ 1 me None 11560 Nov 13 15:43 4.exe

$ ./4.exe lbForth ok words require include required included? create-file !+ ( [defined] [undefined] synonym state! next-name [find [alias imm def com ]alias c-o [then] [if] [else] >[then] nam e<> next-word must-refill forget #body see see' see-line .addr disassemble wid-xt? xt? xt?? .bt .' ?.offset .offset body? words .nt >end ?nt>end traverse-order execut e-xt -rot id. cdump c? dump dump-line dump-cells dump-chars ?dot #digits ? .s search compare /string move cmove> blank -trailing holds +is action-of is defer@ defer! defer alias buffer: within unused u> .r u.r (.r) marker marker! vocs! voc! voc-link! latestxt! context! current! here! marker, vocs, voc-link, latestxt, context, curr ent, voc-link @+ +to to value query roll pick hex false true expect span erase convert ,c" string, (c") string+ (?do) :noname 2r@ 0> 0<> .( (number) 4drop @str !str s tr >number u>number c>number 1/string base+ find word skip uncount accept (quit) rp\ terminal-input terminal-source ok terminal-refill key tib evaluate nr> n>r strin g-input string-source string-refill / /mod fm/mod sm/rem dabs dnegate m um d+- d+ u+d u< spaces #> sign #s # hold <# hld base/mod pad s>d max fill environment env ironment? env-query env-words search-wordlist j r+ >resolve@ leaves variable depth decimal char+ chars 2swap 2over 2! 2@ 2/ mod / postpone-number ." . u. (.) digit ?.

Cheers,

phiho

larsbrinkhoff commented 3 years ago

Glad you could make it work!