From 8aa07234afff05f5491cd00ce239cea2349f9b9c Mon Sep 17 00:00:00 2001 From: GuBerAtHome Date: Mon, 30 Dec 2024 21:06:13 +0100 Subject: [PATCH 1/9] bugfix: removed warnings in WriteCellBigEndian() for 32bit builds --- csrc/pf_save.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/csrc/pf_save.c b/csrc/pf_save.c index 73c44e6..baa539d 100644 --- a/csrc/pf_save.c +++ b/csrc/pf_save.c @@ -218,13 +218,12 @@ void WriteCellBigEndian( uint8_t *addr, ucell_t data ) { /* Write should be in order of increasing address * to optimize for burst writes to DRAM. */ - if( sizeof(ucell_t) == 8 ) - { + #if (LONG_MAX > 2147483647) /* LONG_MAX from limits.h: values > 2^31-1 indicates 64bit ints */ *addr++ = (uint8_t) (data>>56); *addr++ = (uint8_t) (data>>48); *addr++ = (uint8_t) (data>>40); *addr++ = (uint8_t) (data>>32); - } + #endif *addr++ = (uint8_t) (data>>24); *addr++ = (uint8_t) (data>>16); *addr++ = (uint8_t) (data>>8); From 3efb3b370d0c95a97bec018213df21c2e388baed Mon Sep 17 00:00:00 2001 From: GuBerAtHome Date: Tue, 31 Dec 2024 09:27:09 +0100 Subject: [PATCH 2/9] feature: custom demo cf_demo1 --- custom/01-be-gone/cf_demo1.c | 99 ++++++++++++++++++++++++++++++++++ custom/01-be-gone/cf_helpers.h | 42 +++++++++++++++ custom/01-be-gone/go.sh | 53 ++++++++++++++++++ 3 files changed, 194 insertions(+) create mode 100644 custom/01-be-gone/cf_demo1.c create mode 100644 custom/01-be-gone/cf_helpers.h create mode 100644 custom/01-be-gone/go.sh diff --git a/custom/01-be-gone/cf_demo1.c b/custom/01-be-gone/cf_demo1.c new file mode 100644 index 0000000..e4691ec --- /dev/null +++ b/custom/01-be-gone/cf_demo1.c @@ -0,0 +1,99 @@ +#include "pf_all.h" /* lots of stuff */ +#include "cf_helpers.h" /* panic, safeAlloc, to_C_string, {fprintf, stderr, ...} */ +#include /* errno */ + +/* + * put forward declarations here if necessary +*/ + + +/**************************************************************** +** Step 1: Put your own special glue routines here +** or link them in from another file or library. +****************************************************************/ + +/* exported functions */ + +static cell_t f4711( cell_t Val ) +{/* a quick way to check that custom worlds are available + */ + return 11 + 47*Val; +} + +static cell_t be_gone( cell_t fileName, cell_t fnLen ) +{/* Demonstrates passing strings from PForth to C. + Interprets the passed strings as file name and tries to delete the file. + Returns 0 or errno + */ + int res; + char* buf = to_C_string( fileName, fnLen ); + res = remove(buf); /* delete file in file system */ + if( res!=0 ) + res = errno; + free(buf); + return res; +} + + +/**************************************************************** +** Step 2: Create CustomFunctionTable. +** Do not change the name of CustomFunctionTable! +** It is used by the pForth kernel. +****************************************************************/ + +#ifdef PF_NO_GLOBAL_INIT +/****************** +** If your loader does not support global initialization, then you +** must define PF_NO_GLOBAL_INIT and provide a function to fill +** the table. Some embedded system loaders require this! +** Do not change the name of LoadCustomFunctionTable()! +** It is called by the pForth kernel. +*/ +#define NUM_CUSTOM_FUNCTIONS (2) +CFunc0 CustomFunctionTable[NUM_CUSTOM_FUNCTIONS]; + +Err LoadCustomFunctionTable( void ) +{ + CustomFunctionTable[0] = f4711; + CustomFunctionTable[1] = be_gone; + return 0; +} + +#else +/****************** +** If your loader supports global initialization (most do.) then just +** create the table like this. +*/ +CFunc0 CustomFunctionTable[] = +{ + (CFunc0) f4711, + (CFunc0) be_gone +}; +#endif + + +/**************************************************************** +** Step 3: Add custom functions to the dictionary. +** Do not change the name of CompileCustomFunctions! +** It is called by the pForth kernel. +****************************************************************/ + +#if (!defined(PF_NO_INIT)) && (!defined(PF_NO_SHELL)) +Err CompileCustomFunctions( void ) +{ + Err err; + int i = 0; +/* Compile Forth words that call your custom functions. +** Make sure order of functions matches that in LoadCustomFunctionTable(). +** Parameters are: Name in UPPER CASE, Function, Index, Mode, NumParams +*/ + err = CreateGlueToC( "F4711" , i++, C_RETURNS_VALUE, 1 ); + if( err < 0 ) return err; + err = CreateGlueToC( "BE-GONE", i++, C_RETURNS_VALUE, 2 ); + if( err < 0 ) return err; + + return 0; +} +#else +Err CompileCustomFunctions( void ) { return 0; } +#endif diff --git a/custom/01-be-gone/cf_helpers.h b/custom/01-be-gone/cf_helpers.h new file mode 100644 index 0000000..6de3dff --- /dev/null +++ b/custom/01-be-gone/cf_helpers.h @@ -0,0 +1,42 @@ +/* custom code for pforth (hence Custom Forth = cf) + This is a hack and for demonstration purposes only. + It simplifies a few things (like patching of Makefile) + but violates rules for production C code (e.g placing definitions in header files and terminating at the 1st sign of trouble). + Defines helper functions for several examples. +*/ + +#ifndef CF_HELPERS_H +#define CF_HELPERS_H + +#include /* fprintf() */ +#include /* exit() */ + +static void panic( const char* exitMsg ) +{/* Terminates program with panic message on stderr + Beware: might mess up the terminal sometimes :-/ (restarting pforth works fine though) + */ + fprintf(stderr, "\n====> panic! about to exit: %s\n", exitMsg); + exit(1); +} + +static void* safeAlloc( size_t bytes ) +{/* allocate memory and panic if that did not succees + */ + void* result = malloc(bytes); /* TODO: replace by calloc() ?! */ + if(result==NULL) + panic("can not allocate memory!"); + return result; +} + +static char* to_C_string( cell_t strData, cell_t iStrLen ) +{/* copy PForth string to C-string (zero terminated) + Don't forget to free() the result! + TODO: check if there is already defined a similar function in pforth + */ + char* buf = safeAlloc(iStrLen+1); + memcpy( buf, (void*)strData, iStrLen ); + buf[iStrLen] = 0; + return buf; +} + +#endif diff --git a/custom/01-be-gone/go.sh b/custom/01-be-gone/go.sh new file mode 100644 index 0000000..9f591b7 --- /dev/null +++ b/custom/01-be-gone/go.sh @@ -0,0 +1,53 @@ +#!/bin/sh + +MAKE_CMD="make" # on Linux, MSYS2-Cygwin +#MAKE_CMD="gmake" # on FreeBSD?, NetBSD + + +# Compile pForth with custom code and show that this works. +# We assume a posix shell and system (but adaption should be easy to others). +# This patches the existing source tree and might create confusion when not used on separate Git branch in case an error occurs. + +# save original C sources and copy demo sources. Thus we do not need to change the make file. +mv ../../csrc/pfcustom.c ../../csrc/pfcustom_c.original +cp cf_helpers.h ../../csrc/ +cp cf_demo1.c ../../csrc/pfcustom.c # + +# make pforth (skip standalone executable) +# We would not even need to define DPF_USER_CUSTOM since it is only used in the original pfcustom.c we overwrote. +cd ../../platforms/unix/ +DPF_USER_CUSTOM="1" $MAKE_CMD clean pforth.dic + +# create a nuisance to delete +mv ../../csrc/cf_helpers.h ./terrible_nuisance.asm + +echo +echo "---------------------------" +echo "show that custom code works" +echo "---------------------------" +cat >demo1.fth << EOF +." f4711( 0, 100 ) = " +0 f4711 . +100 f4711 . +CR + +." be-gone: " +s" terrible_nuisance.asm" be-gone 0= dup +if + ." works." + drop +else + ." returns error=" . +then CR +EOF +./pforth -q demo1.fth + +# restore original source tree +rm demo1.fth +mv ../../csrc/pfcustom_c.original ../../csrc/pfcustom.c +$MAKE_CMD clean + +echo +echo "-----------------" +echo "That's all folks!" +echo "-----------------" From 70e74ae003ae5445b9b7a4216b56d272c65ce098 Mon Sep 17 00:00:00 2001 From: GuBerAtHome Date: Tue, 31 Dec 2024 17:38:04 +0100 Subject: [PATCH 3/9] tested and fixed cf_demo1 on MSYS2, Linux, FreeBSD, NetBSD --- custom/01-be-gone/cf_demo1.c | 2 +- custom/01-be-gone/demo.fth | 18 +++++++++ custom/01-be-gone/go.sh | 56 +++++++++++++++------------- custom/{01-be-gone => }/cf_helpers.h | 0 4 files changed, 49 insertions(+), 27 deletions(-) create mode 100644 custom/01-be-gone/demo.fth rename custom/{01-be-gone => }/cf_helpers.h (100%) diff --git a/custom/01-be-gone/cf_demo1.c b/custom/01-be-gone/cf_demo1.c index e4691ec..c6c8c55 100644 --- a/custom/01-be-gone/cf_demo1.c +++ b/custom/01-be-gone/cf_demo1.c @@ -28,7 +28,7 @@ static cell_t be_gone( cell_t fileName, cell_t fnLen ) int res; char* buf = to_C_string( fileName, fnLen ); res = remove(buf); /* delete file in file system */ - if( res!=0 ) + if( res!=0 && errno!=0 ) res = errno; free(buf); return res; diff --git a/custom/01-be-gone/demo.fth b/custom/01-be-gone/demo.fth new file mode 100644 index 0000000..1b6a99e --- /dev/null +++ b/custom/01-be-gone/demo.fth @@ -0,0 +1,18 @@ +\ f4711 is a clear indicator that compilation of custom functions was successful +." f4711( 0, 1, 10, 100, 1000 ) = ( " + 0 f4711 . ." , " + 1 f4711 . ." , " + 10 f4711 . ." , " + 100 f4711 . ." , " +1000 f4711 . ." )" +CR + +\ example of passing passing strings from PForth to custom C code +." be-gone: " +s" terrible_nuisance.asm" be-gone dup 0= +if + ." works." + drop +else + ." returns error=" . +then CR diff --git a/custom/01-be-gone/go.sh b/custom/01-be-gone/go.sh index 9f591b7..65a66f1 100644 --- a/custom/01-be-gone/go.sh +++ b/custom/01-be-gone/go.sh @@ -1,16 +1,33 @@ #!/bin/sh -MAKE_CMD="make" # on Linux, MSYS2-Cygwin -#MAKE_CMD="gmake" # on FreeBSD?, NetBSD - - -# Compile pForth with custom code and show that this works. +# Compile pForth with custom code and show that this works. # We assume a posix shell and system (but adaption should be easy to others). -# This patches the existing source tree and might create confusion when not used on separate Git branch in case an error occurs. +# Note: This is the easiest solution but ignores PForths best practices set up in pfcustom.c +# Warning: This patches the existing source tree and might create confusion when not used on separate Git branch in case an error occurs. +# Tested on MSYS2-Cygwin, Linux, FreeBSD (X86_64 architecture each), NetBSD (i386 architecture) + +os=`uname -o 2>/dev/null` +if test -z "$os" ; then + # NetBSD-uname does not implement '-o' option + os=`uname -s` +fi +case "$os" in + "FreeBSD") + CC="clang" + export CC + MAKE_CMD="gmake" + ;; + "NetBSD") + MAKE_CMD="gmake" + ;; + *) # e.g. "Msys" | "GNU/Linux" + MAKE_CMD="make" + ;; +esac # save original C sources and copy demo sources. Thus we do not need to change the make file. mv ../../csrc/pfcustom.c ../../csrc/pfcustom_c.original -cp cf_helpers.h ../../csrc/ +cp ../cf_helpers.h ../../csrc/ cp cf_demo1.c ../../csrc/pfcustom.c # # make pforth (skip standalone executable) @@ -25,25 +42,12 @@ echo echo "---------------------------" echo "show that custom code works" echo "---------------------------" -cat >demo1.fth << EOF -." f4711( 0, 100 ) = " -0 f4711 . -100 f4711 . -CR - -." be-gone: " -s" terrible_nuisance.asm" be-gone 0= dup -if - ." works." - drop -else - ." returns error=" . -then CR -EOF -./pforth -q demo1.fth - -# restore original source tree -rm demo1.fth +./pforth -q ../../custom/01-be-gone/demo.fth + +echo +echo "----------------------------" +echo "restore original source tree" +echo "----------------------------" mv ../../csrc/pfcustom_c.original ../../csrc/pfcustom.c $MAKE_CMD clean diff --git a/custom/01-be-gone/cf_helpers.h b/custom/cf_helpers.h similarity index 100% rename from custom/01-be-gone/cf_helpers.h rename to custom/cf_helpers.h From 1bd41cfce35f5409fd394ddf66563d177e41cf83 Mon Sep 17 00:00:00 2001 From: GuBerAtHome Date: Tue, 31 Dec 2024 22:22:41 +0100 Subject: [PATCH 4/9] feature: new custom code handling for unix build --- custom/01-be-gone/{go.sh => go-v0.sh} | 7 ++-- custom/01-be-gone/go-v1.sh | 56 +++++++++++++++++++++++++++ platforms/unix/Makefile | 9 ++++- 3 files changed, 68 insertions(+), 4 deletions(-) rename custom/01-be-gone/{go.sh => go-v0.sh} (88%) create mode 100644 custom/01-be-gone/go-v1.sh diff --git a/custom/01-be-gone/go.sh b/custom/01-be-gone/go-v0.sh similarity index 88% rename from custom/01-be-gone/go.sh rename to custom/01-be-gone/go-v0.sh index 65a66f1..867da58 100644 --- a/custom/01-be-gone/go.sh +++ b/custom/01-be-gone/go-v0.sh @@ -13,6 +13,7 @@ if test -z "$os" ; then fi case "$os" in "FreeBSD") + # TODO: remove next line once Makefile has been updated accordingly (pull request pending) CC="clang" export CC MAKE_CMD="gmake" @@ -27,13 +28,13 @@ esac # save original C sources and copy demo sources. Thus we do not need to change the make file. mv ../../csrc/pfcustom.c ../../csrc/pfcustom_c.original -cp ../cf_helpers.h ../../csrc/ -cp cf_demo1.c ../../csrc/pfcustom.c # +cp ../cf_helpers.h ../../csrc/ +cp cf_demo1.c ../../csrc/pfcustom.c # make pforth (skip standalone executable) # We would not even need to define DPF_USER_CUSTOM since it is only used in the original pfcustom.c we overwrote. cd ../../platforms/unix/ -DPF_USER_CUSTOM="1" $MAKE_CMD clean pforth.dic +PF_USER_CUSTOM="1" $MAKE_CMD clean pforth.dic # create a nuisance to delete mv ../../csrc/cf_helpers.h ./terrible_nuisance.asm diff --git a/custom/01-be-gone/go-v1.sh b/custom/01-be-gone/go-v1.sh new file mode 100644 index 0000000..85c7aa7 --- /dev/null +++ b/custom/01-be-gone/go-v1.sh @@ -0,0 +1,56 @@ +#!/bin/sh + +# Compile pForth with custom code and show that this works. +# We assume a posix shell and system (but adaption should be easy to others). +# This improved version only compiles the custom code defined in CF_SOURCES. +# Warning: This patches the existing source tree and might create confusion when not used on separate Git branch in case an error occurs. +# Tested on MSYS2-Cygwin, Linux, FreeBSD (X86_64 architecture each), NetBSD (i386 architecture) + +os=`uname -o 2>/dev/null` +if test -z "$os" ; then + # NetBSD-uname does not implement '-o' option + os=`uname -s` +fi +case "$os" in + "FreeBSD") + # TODO: remove next line once Makefile has been updated accordingly (pull request pending) + CC="clang" + export CC + MAKE_CMD="gmake" + ;; + "NetBSD") + MAKE_CMD="gmake" + ;; + *) # e.g. "Msys" | "GNU/Linux" + MAKE_CMD="make" + ;; +esac + +# copy demo sources. Thus we do not need to change the make file. +cp ../cf_helpers.h ../../csrc/ +cp cf_demo1.c ../../csrc/ + +# make pforth (skip standalone executable) +cd ../../platforms/unix/ +CF_SOURCES="cf_demo1.c" $MAKE_CMD clean pforth.dic + +# create a nuisance to delete +mv ../../csrc/cf_helpers.h ./terrible_nuisance.asm + +echo +echo "---------------------------" +echo "show that custom code works" +echo "---------------------------" +./pforth -q ../../custom/01-be-gone/demo.fth + +echo +echo "----------------------------" +echo "restore original source tree" +echo "----------------------------" +rm ../../csrc/cf_demo1.c +CF_SOURCES="cf_demo1.c" $MAKE_CMD clean + +echo +echo "-----------------" +echo "That's all folks!" +echo "-----------------" diff --git a/platforms/unix/Makefile b/platforms/unix/Makefile index 5bcc164..2eb9e1c 100644 --- a/platforms/unix/Makefile +++ b/platforms/unix/Makefile @@ -39,6 +39,13 @@ FULL_WARNINGS = \ -Wmissing-prototypes \ -Wmissing-declarations +# custom code options +ifndef CF_SOURCES + # note: setting CF_PARAM does not help: It only deactivates all definitions in pfcustom.c, which we only include when no custom code is compiled! + # We do not remove CF_PARAM from code and documentation until we can verify that Makefiles continue to work. + CF_SOURCES=pfcustom.c +endif + DEBUGOPTS = -g CCOPTS = $(WIDTHOPT) -x c -O2 $(FULL_WARNINGS) $(EXTRA_CCOPTS) $(DEBUGOPTS) @@ -70,7 +77,7 @@ PFINCLUDES = pf_all.h pf_cglue.h pf_clib.h pf_core.h pf_float.h \ pfcompil.h pfinnrfp.h pforth.h PFBASESOURCE = pf_cglue.c pf_clib.c pf_core.c pf_inner.c \ pf_io.c pf_io_none.c pf_main.c pf_mem.c pf_save.c \ - pf_text.c pf_words.c pfcompil.c pfcustom.c + pf_text.c pf_words.c pfcompil.c $(CF_SOURCES) PFSOURCE = $(PFBASESOURCE) $(IO_SOURCE) VPATH = .:$(CSRCDIR):$(CSRCDIR)/posix:$(CSRCDIR)/stdio:$(CSRCDIR)/win32_console:$(CSRCDIR)/win32 From 6889a15445216f4f1714e10c418276378f21d428 Mon Sep 17 00:00:00 2001 From: GuBerAtHome Date: Tue, 31 Dec 2024 23:25:14 +0100 Subject: [PATCH 5/9] bugfix: latest merge from upstream simplifies FreeBSD handling --- custom/01-be-gone/go-v0.sh | 10 ++-------- custom/01-be-gone/go-v1.sh | 10 ++-------- 2 files changed, 4 insertions(+), 16 deletions(-) diff --git a/custom/01-be-gone/go-v0.sh b/custom/01-be-gone/go-v0.sh index 867da58..c27410c 100644 --- a/custom/01-be-gone/go-v0.sh +++ b/custom/01-be-gone/go-v0.sh @@ -12,18 +12,12 @@ if test -z "$os" ; then os=`uname -s` fi case "$os" in - "FreeBSD") - # TODO: remove next line once Makefile has been updated accordingly (pull request pending) - CC="clang" - export CC + "FreeBSD" | "NetBSD") MAKE_CMD="gmake" ;; - "NetBSD") - MAKE_CMD="gmake" - ;; *) # e.g. "Msys" | "GNU/Linux" MAKE_CMD="make" - ;; + ;; esac # save original C sources and copy demo sources. Thus we do not need to change the make file. diff --git a/custom/01-be-gone/go-v1.sh b/custom/01-be-gone/go-v1.sh index 85c7aa7..52bf5d6 100644 --- a/custom/01-be-gone/go-v1.sh +++ b/custom/01-be-gone/go-v1.sh @@ -12,18 +12,12 @@ if test -z "$os" ; then os=`uname -s` fi case "$os" in - "FreeBSD") - # TODO: remove next line once Makefile has been updated accordingly (pull request pending) - CC="clang" - export CC + "FreeBSD" | "NetBSD") MAKE_CMD="gmake" ;; - "NetBSD") - MAKE_CMD="gmake" - ;; *) # e.g. "Msys" | "GNU/Linux" MAKE_CMD="make" - ;; + ;; esac # copy demo sources. Thus we do not need to change the make file. From aeff369c9cccb3952b105a68e4de0dd079d532de Mon Sep 17 00:00:00 2001 From: GuBerAtHome Date: Tue, 31 Dec 2024 23:48:10 +0100 Subject: [PATCH 6/9] refactored get-make-cmd to separate shell script --- custom/01-be-gone/get-make-cmd.sh | 16 ++++++++++++++++ custom/01-be-gone/go-v0.sh | 22 ++++++---------------- custom/01-be-gone/go-v1.sh | 22 +++++++--------------- 3 files changed, 29 insertions(+), 31 deletions(-) create mode 100644 custom/01-be-gone/get-make-cmd.sh diff --git a/custom/01-be-gone/get-make-cmd.sh b/custom/01-be-gone/get-make-cmd.sh new file mode 100644 index 0000000..135f9f4 --- /dev/null +++ b/custom/01-be-gone/get-make-cmd.sh @@ -0,0 +1,16 @@ +#!/bin/sh + +os=`uname -o 2>/dev/null` +if test -z "$os" ; then + # NetBSD-uname does not implement '-o' option + os=`uname -s` +fi + +case "$os" in + "FreeBSD" | "NetBSD") + echo "gmake" + ;; + *) # e.g. "Msys" | "GNU/Linux" + echo "make" + ;; +esac diff --git a/custom/01-be-gone/go-v0.sh b/custom/01-be-gone/go-v0.sh index c27410c..736283c 100644 --- a/custom/01-be-gone/go-v0.sh +++ b/custom/01-be-gone/go-v0.sh @@ -6,28 +6,18 @@ # Warning: This patches the existing source tree and might create confusion when not used on separate Git branch in case an error occurs. # Tested on MSYS2-Cygwin, Linux, FreeBSD (X86_64 architecture each), NetBSD (i386 architecture) -os=`uname -o 2>/dev/null` -if test -z "$os" ; then - # NetBSD-uname does not implement '-o' option - os=`uname -s` -fi -case "$os" in - "FreeBSD" | "NetBSD") - MAKE_CMD="gmake" - ;; - *) # e.g. "Msys" | "GNU/Linux" - MAKE_CMD="make" - ;; -esac - # save original C sources and copy demo sources. Thus we do not need to change the make file. mv ../../csrc/pfcustom.c ../../csrc/pfcustom_c.original cp ../cf_helpers.h ../../csrc/ cp cf_demo1.c ../../csrc/pfcustom.c -# make pforth (skip standalone executable) -# We would not even need to define DPF_USER_CUSTOM since it is only used in the original pfcustom.c we overwrote. +echo +echo "----------------------------------------" +echo "make pforth (skip standalone executable)" +echo "----------------------------------------" +MAKE_CMD=`./get-make-cmd.sh` cd ../../platforms/unix/ +# We would not even need to define DPF_USER_CUSTOM since it is only used in the original pfcustom.c we overwrote. PF_USER_CUSTOM="1" $MAKE_CMD clean pforth.dic # create a nuisance to delete diff --git a/custom/01-be-gone/go-v1.sh b/custom/01-be-gone/go-v1.sh index 52bf5d6..8ec6ab1 100644 --- a/custom/01-be-gone/go-v1.sh +++ b/custom/01-be-gone/go-v1.sh @@ -6,26 +6,18 @@ # Warning: This patches the existing source tree and might create confusion when not used on separate Git branch in case an error occurs. # Tested on MSYS2-Cygwin, Linux, FreeBSD (X86_64 architecture each), NetBSD (i386 architecture) -os=`uname -o 2>/dev/null` -if test -z "$os" ; then - # NetBSD-uname does not implement '-o' option - os=`uname -s` -fi -case "$os" in - "FreeBSD" | "NetBSD") - MAKE_CMD="gmake" - ;; - *) # e.g. "Msys" | "GNU/Linux" - MAKE_CMD="make" - ;; -esac - # copy demo sources. Thus we do not need to change the make file. + cp ../cf_helpers.h ../../csrc/ cp cf_demo1.c ../../csrc/ -# make pforth (skip standalone executable) +echo +echo "----------------------------------------" +echo "make pforth (skip standalone executable)" +echo "----------------------------------------" +MAKE_CMD=`./get-make-cmd.sh` cd ../../platforms/unix/ + CF_SOURCES="cf_demo1.c" $MAKE_CMD clean pforth.dic # create a nuisance to delete From 5a11969c9395a2ac83f3eba06c4b5038b7dd8a98 Mon Sep 17 00:00:00 2001 From: GuBerAtHome Date: Wed, 1 Jan 2025 21:12:21 +0100 Subject: [PATCH 7/9] feature: implement (argc,argv) words as custom-02 --- custom/01-be-gone/go-v1.sh | 2 +- custom/02-argc-argv/cf_demo2.c | 122 ++++++++++++++++++++++++ custom/02-argc-argv/demo.fth | 7 ++ custom/02-argc-argv/diff-pf_main.c | 25 +++++ custom/02-argc-argv/go-v1.sh | 41 ++++++++ custom/{01-be-gone => }/get-make-cmd.sh | 0 6 files changed, 196 insertions(+), 1 deletion(-) create mode 100644 custom/02-argc-argv/cf_demo2.c create mode 100644 custom/02-argc-argv/demo.fth create mode 100644 custom/02-argc-argv/diff-pf_main.c create mode 100644 custom/02-argc-argv/go-v1.sh rename custom/{01-be-gone => }/get-make-cmd.sh (100%) diff --git a/custom/01-be-gone/go-v1.sh b/custom/01-be-gone/go-v1.sh index 8ec6ab1..cb89e04 100644 --- a/custom/01-be-gone/go-v1.sh +++ b/custom/01-be-gone/go-v1.sh @@ -15,7 +15,7 @@ echo echo "----------------------------------------" echo "make pforth (skip standalone executable)" echo "----------------------------------------" -MAKE_CMD=`./get-make-cmd.sh` +MAKE_CMD=`../get-make-cmd.sh` cd ../../platforms/unix/ CF_SOURCES="cf_demo1.c" $MAKE_CMD clean pforth.dic diff --git a/custom/02-argc-argv/cf_demo2.c b/custom/02-argc-argv/cf_demo2.c new file mode 100644 index 0000000..5d475cb --- /dev/null +++ b/custom/02-argc-argv/cf_demo2.c @@ -0,0 +1,122 @@ +#include "pf_all.h" /* lots of stuff */ +#include /* calloc */ +#include /* strlen */ + +/* + * put forward declarations here if necessary +*/ + +void fillScriptParams( int startItem, int NoItems, char *argv[] ); + +/**************************************************************** +** Step 1: Put your own special glue routines here +** or link them in from another file or library. +****************************************************************/ + +/* exported functions */ + +static cell_t cf_f4711( cell_t Val ) +{/* a quick way to check that custom words are available + */ + return 11 + 47*Val; +} + + +struct PascalString { + char* data; + size_t len; +} ; +typedef struct PascalString* PascalStringPtr; + +static struct { + PascalStringPtr args; + int cnt; +} ScriptParams; + +void fillScriptParams( int startItem, int NoItems, char *argv[] ) { + int i; + if( startItem < NoItems ) { + ScriptParams.cnt = NoItems - startItem; + ScriptParams.args = (PascalStringPtr) calloc( ScriptParams.cnt, sizeof(struct PascalString) ); + for( i=0; i caddr len' OR in case of error '--> 0 -1' + */ cell_t len = -1; /* assume something will go wrong */ + cell_t data = 0; /* dito */ + if( 0<=paramNo && paramNo < ScriptParams.cnt ) { + data = (cell_t) ScriptParams.args[paramNo].data; + len = ScriptParams.args[paramNo].len; + } + PUSH_DATA_STACK( (cell_t) data ); + return len; +} + + + +/**************************************************************** +** Step 2: Create CustomFunctionTable. +** Do not change the name of CustomFunctionTable! +** It is used by the pForth kernel. +****************************************************************/ + +#ifdef PF_NO_GLOBAL_INIT +#define NUM_CUSTOM_FUNCTIONS (3) +CFunc0 CustomFunctionTable[NUM_CUSTOM_FUNCTIONS]; + +Err LoadCustomFunctionTable( void ) +{ + CustomFunctionTable[0] = cf_f4711; + CustomFunctionTable[1] = cf_argc; + CustomFunctionTable[2] = cf_argv; + return 0; +} + +#else +CFunc0 CustomFunctionTable[] = +{ + (CFunc0) cf_f4711, + (CFunc0) cf_argc, + (CFunc0) cf_argv +}; +#endif + + +/**************************************************************** +** Step 3: Add custom functions to the dictionary. +** Do not change the name of CompileCustomFunctions! +** It is called by the pForth kernel. +****************************************************************/ + +#if (!defined(PF_NO_INIT)) && (!defined(PF_NO_SHELL)) +Err CompileCustomFunctions( void ) +{ + Err err; + int i = 0; +/* Compile Forth words that call your custom functions. +** Make sure order of functions matches that in LoadCustomFunctionTable(). +** Parameters are: Name in UPPER CASE, Function, Index, Mode, NumParams +*/ + err = CreateGlueToC( "F4711", i++, C_RETURNS_VALUE, 1 ); + if( err < 0 ) return err; + err = CreateGlueToC( "ARGC" , i++, C_RETURNS_VALUE, 0 ); + if( err < 0 ) return err; + err = CreateGlueToC( "ARGV" , i++, C_RETURNS_VALUE, 1 ); + if( err < 0 ) return err; + + return 0; +} +#else +Err CompileCustomFunctions( void ) { return 0; } +#endif diff --git a/custom/02-argc-argv/demo.fth b/custom/02-argc-argv/demo.fth new file mode 100644 index 0000000..c6cbcb9 --- /dev/null +++ b/custom/02-argc-argv/demo.fth @@ -0,0 +1,7 @@ +argc ." argc=" . cr + +argc 0 do + i dup ." + argv[" (.) type ." ] = '" + argv type ." '" cr +loop diff --git a/custom/02-argc-argv/diff-pf_main.c b/custom/02-argc-argv/diff-pf_main.c new file mode 100644 index 0000000..673b8bb --- /dev/null +++ b/custom/02-argc-argv/diff-pf_main.c @@ -0,0 +1,25 @@ +*** old-pf_main.c Wed Jan 1 21:13:11 2025 +--- new-pf_main.c Wed Jan 1 21:11:33 2025 +*************** +*** 58,63 **** +--- 58,64 ---- + } + #else + ++ void fillScriptParams( int startItem, int NoItems, char *argv[] ); /* forward declaration for cf_demo2.c */ + int main( int argc, char **argv ) + { + #ifdef PF_STATIC_DIC +*************** +*** 90,95 **** +--- 91,100 ---- + c = *s++; + switch(c) + { ++ case '-': /* call code in cf_demo2.c then skip remaining arguments */ ++ fillScriptParams( i+1, argc, argv ); ++ i = argc; ++ break; + case 'i': + IfInit = TRUE; + DicName = NULL; diff --git a/custom/02-argc-argv/go-v1.sh b/custom/02-argc-argv/go-v1.sh new file mode 100644 index 0000000..73dcc02 --- /dev/null +++ b/custom/02-argc-argv/go-v1.sh @@ -0,0 +1,41 @@ +#!/bin/sh + +# Compile pForth with custom code and show that this works. +# We assume a posix shell and system (but adaption should be easy to others). +# This improved version only compiles the custom code defined in CF_SOURCES. +# Warning: This patches the existing source tree and might create confusion when not used on separate Git branch in case an error occurs. +# Tested on MSYS2-Cygwin, Linux, FreeBSD (X86_64 architecture each), NetBSD (i386 architecture) + +# copy demo sources. Thus we do not need to change the make file. + +cp cf_demo2.c ../../csrc/ +patch -bcN ../../csrc/pf_main.c diff-pf_main.c +# patch was created via: diff -c old-pf_main.c new-pf_main.c > diff-pf_main.c +# patch options: -b=backup, -c=context-diff (same as in diff), -N=ignore reversed or already applied patches + +echo +echo "----------------------------------------" +echo "make pforth (skip standalone executable)" +echo "----------------------------------------" +MAKE_CMD=`../get-make-cmd.sh` +cd ../../platforms/unix/ +CF_SOURCES="cf_demo2.c" $MAKE_CMD pforth.dic + +echo +echo "---------------------------" +echo "show that custom code works" +echo "---------------------------" +./pforth -q ../../custom/02-argc-argv/demo.fth -- "01: icke dette" 02:kieke 03:mal + +echo +echo "----------------------------" +echo "restore original source tree" +echo "----------------------------" +rm ../../csrc/cf_demo2.c +mv ../../csrc/pf_main.c.orig ../../csrc/pf_main.c +CF_SOURCES="cf_demo2.c" $MAKE_CMD clean + +echo +echo "-----------------" +echo "That's all folks!" +echo "-----------------" diff --git a/custom/01-be-gone/get-make-cmd.sh b/custom/get-make-cmd.sh similarity index 100% rename from custom/01-be-gone/get-make-cmd.sh rename to custom/get-make-cmd.sh From f895142f4004b5bbe271a3f54a235d93bd6c3157 Mon Sep 17 00:00:00 2001 From: GuBerAtHome Date: Wed, 1 Jan 2025 23:03:12 +0100 Subject: [PATCH 8/9] feature: verify output from custom code --- custom/02-argc-argv/demo.correct_output | 4 ++++ custom/02-argc-argv/demo.fth | 4 ++-- custom/02-argc-argv/go-v1.sh | 6 +++++- 3 files changed, 11 insertions(+), 3 deletions(-) create mode 100644 custom/02-argc-argv/demo.correct_output diff --git a/custom/02-argc-argv/demo.correct_output b/custom/02-argc-argv/demo.correct_output new file mode 100644 index 0000000..89af645 --- /dev/null +++ b/custom/02-argc-argv/demo.correct_output @@ -0,0 +1,4 @@ +argc=3 +argv[0] = '01: icke dette' +argv[1] = '02:kieke' +argv[2] = '03:mal' diff --git a/custom/02-argc-argv/demo.fth b/custom/02-argc-argv/demo.fth index c6cbcb9..fb19b5a 100644 --- a/custom/02-argc-argv/demo.fth +++ b/custom/02-argc-argv/demo.fth @@ -1,7 +1,7 @@ argc ." argc=" . cr argc 0 do - i dup ." - argv[" (.) type ." ] = '" + i dup + ." argv[" (.) type ." ] = '" argv type ." '" cr loop diff --git a/custom/02-argc-argv/go-v1.sh b/custom/02-argc-argv/go-v1.sh index 73dcc02..6060e51 100644 --- a/custom/02-argc-argv/go-v1.sh +++ b/custom/02-argc-argv/go-v1.sh @@ -25,12 +25,16 @@ echo echo "---------------------------" echo "show that custom code works" echo "---------------------------" -./pforth -q ../../custom/02-argc-argv/demo.fth -- "01: icke dette" 02:kieke 03:mal +(./pforth -q ../../custom/02-argc-argv/demo.fth -- "01: icke dette" 02:kieke 03:mal) | tee cf-demo.output +echo "- - - - - - - - - - - - - -" +echo "is this the expected output?" +diff -s cf-demo.output ../../custom/02-argc-argv/demo.correct_output echo echo "----------------------------" echo "restore original source tree" echo "----------------------------" +rm cf-demo.output rm ../../csrc/cf_demo2.c mv ../../csrc/pf_main.c.orig ../../csrc/pf_main.c CF_SOURCES="cf_demo2.c" $MAKE_CMD clean From 3e12bbe352f71a901c2a54a76f263a1dd4ccf015 Mon Sep 17 00:00:00 2001 From: GuBerAtHome Date: Wed, 1 Jan 2025 23:24:23 +0100 Subject: [PATCH 9/9] bugfix: readable sample parameters and output --- custom/02-argc-argv/demo.correct_output | 6 +++--- custom/02-argc-argv/go-v1.sh | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/custom/02-argc-argv/demo.correct_output b/custom/02-argc-argv/demo.correct_output index 89af645..8d115ed 100644 --- a/custom/02-argc-argv/demo.correct_output +++ b/custom/02-argc-argv/demo.correct_output @@ -1,4 +1,4 @@ argc=3 -argv[0] = '01: icke dette' -argv[1] = '02:kieke' -argv[2] = '03:mal' +argv[0] = '01: One FORTH to' +argv[1] = '02:find' +argv[2] = '03:them' diff --git a/custom/02-argc-argv/go-v1.sh b/custom/02-argc-argv/go-v1.sh index 6060e51..0e7d9b5 100644 --- a/custom/02-argc-argv/go-v1.sh +++ b/custom/02-argc-argv/go-v1.sh @@ -25,7 +25,7 @@ echo echo "---------------------------" echo "show that custom code works" echo "---------------------------" -(./pforth -q ../../custom/02-argc-argv/demo.fth -- "01: icke dette" 02:kieke 03:mal) | tee cf-demo.output +(./pforth -q ../../custom/02-argc-argv/demo.fth -- "01: One FORTH to" 02:find 03:them) | tee cf-demo.output echo "- - - - - - - - - - - - - -" echo "is this the expected output?" diff -s cf-demo.output ../../custom/02-argc-argv/demo.correct_output