-
Notifications
You must be signed in to change notification settings - Fork 115
Example of adding custom C functions #187
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
guberathome
wants to merge
23
commits into
philburk:master
Choose a base branch
from
guberathome:docu-241230
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 9 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
8aa0723
bugfix: removed warnings in WriteCellBigEndian() for 32bit builds
guberathome cb40ed0
Merge branch 'philburk:master' into fix-gub-241230
guberathome 3efb3b3
feature: custom demo cf_demo1
guberathome 70e74ae
tested and fixed cf_demo1 on MSYS2, Linux, FreeBSD, NetBSD
guberathome 1bd41cf
feature: new custom code handling for unix build
guberathome 09dffd0
Merge branch 'philburk:master' into docu-241230
guberathome 6889a15
bugfix: latest merge from upstream simplifies FreeBSD handling
guberathome aeff369
refactored get-make-cmd to separate shell script
guberathome 20fdba6
merge: manually resolved conflicts with upstream repo
guberathome f605544
Merge branch 'philburk:master' into docu-241230
guberathome 866422e
from-codereview: moved to examples/custom/ folder
guberathome 03a4d0a
from-codereview: replaced BE-GONE by FILE-INFO, fixed go-v1.sh
guberathome 122ab9b
from-codereview: removed panic() and safeAlloc()
guberathome 409cee3
from-codereview: renamed Makefile variable to CUSTOM_SOURCES
guberathome 6daae79
from-codereview: removed go-v0.sh
guberathome ea826e9
fixed typo
guberathome e0dd86f
bugfix: removed superfluos code for MSYS
guberathome 1d3e5e4
Merge branch 'philburk:master' into docu-241230
guberathome 82b212b
changes from review
guberathome 5cb19e1
free C buffer
guberathome b9e5235
added description for FileInfo()
guberathome 912172e
use result of asprintf instead of calling strlen
guberathome 128666c
Merge branch 'master' into docu-241230
guberathome File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| #include "pf_all.h" /* lots of stuff */ | ||
| #include "cf_helpers.h" /* panic, safeAlloc, to_C_string, <stdio.h->{fprintf, stderr, ...} */ | ||
| #include <errno.h> /* 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 && errno!=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 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
|
guberathome marked this conversation as resolved.
Outdated
|
||
| ." be-gone: " | ||
| s" terrible_nuisance.asm" be-gone dup 0= | ||
| if | ||
| ." works." | ||
| drop | ||
| else | ||
| ." returns error=" . | ||
| then CR | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| #!/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). | ||
| # 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) | ||
|
|
||
| # 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 | ||
|
guberathome marked this conversation as resolved.
Outdated
|
||
|
|
||
| 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 | ||
| 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 "----------------------------" | ||
| mv ../../csrc/pfcustom_c.original ../../csrc/pfcustom.c | ||
| $MAKE_CMD clean | ||
|
|
||
| echo | ||
| echo "-----------------" | ||
| echo "That's all folks!" | ||
| echo "-----------------" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| #!/bin/sh | ||
|
|
||
| # Compile pForth with custom code and show that this works. | ||
|
guberathome marked this conversation as resolved.
|
||
| # 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_helpers.h ../../csrc/ | ||
| cp cf_demo1.c ../../csrc/ | ||
|
|
||
| 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 | ||
| 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 "-----------------" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 <stdio.h> /* fprintf() */ | ||
| #include <stdlib.h> /* 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 | ||
| */ | ||
|
guberathome marked this conversation as resolved.
Outdated
|
||
| char* buf = safeAlloc(iStrLen+1); | ||
| memcpy( buf, (void*)strData, iStrLen ); | ||
| buf[iStrLen] = 0; | ||
| return buf; | ||
| } | ||
|
|
||
| #endif | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.