Callbacks in gforth-0.6.9-20080716

This is a discussion on Callbacks in gforth-0.6.9-20080716 within the Forth forums in Programming Languages category; Hello, I have trouble making callbacks work in gforth-0.6.9-20080716. So far, I can use libcc to build both the callback and the caller function, and use them in gforth. But I cannot make the final step to lib.fs callbacks. Apparently, I'm not passing the callback right to the C code. The system is an Ubuntu 8.04, with my self-made package of gforth, out of combining the existing package and the gforth-0.6.9-20080716 upstream ("make check" passes). By the way, I'd be glad to give it to anyone who could help make it land in Debian. Can anyone (yes, Anton, I'm looking ...

Go Back   Application Development Forum > Programming Languages > Forth

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 08-30-2008, 05:56 PM
ddaa
Guest
 
Default Callbacks in gforth-0.6.9-20080716

Hello, I have trouble making callbacks work in gforth-0.6.9-20080716.

So far, I can use libcc to build both the callback and the caller
function, and use them in gforth. But I cannot make the final step to
lib.fs callbacks. Apparently, I'm not passing the callback right to
the C code.

The system is an Ubuntu 8.04, with my self-made package of gforth, out
of combining the existing package and the gforth-0.6.9-20080716
upstream ("make check" passes). By the way, I'd be glad to give it to
anyone who could help make it land in Debian.

Can anyone (yes, Anton, I'm looking at you) help me make it work?

callback-hello.fs
-----8<----------8<----------8<----------8<----------8<----------8<-----
\c #include <stdio.h>
\c typedef int (*int_func_int_int) (int, int);
\c int into_callback(int_func_int_int cb, int a, int b) {
\c printf("into_callback %x %d %d\n", cb, a, b);
\c int result = (*cb)(a, b);
\c printf("returning %d\n", result);
\c return result; }
\c int foreign_plus(int a, int b) {
\c printf("foreign_plus %d %d\n", a, b); return a + b ; }
\c #define FOREIGN_PLUS_PTR() (&foreign_plus)
c-function into-callback into_callback func n n -- n
c-function foreign-plus-ptr FOREIGN_PLUS_PTR -- func

require lib.fs cr

callback 2:1 (int) int int callback;

: forth-plus ( a b -- c )
cr ." Testing callback"
cr ." arguments: " .s
cr ." result " + .s cr ;
' forth-plus 2:1 forth-plus-cb

: foreign-test
." foreign-test"
foreign-plus-ptr 2 2 ~~
into-callback . ~~ ;

: forth-test
." forth-test"
forth-plus-cb 2 2 ~~
into-callback . ~~ ;

foreign-test
forth-test
bye
-----8<----------8<----------8<----------8<----------8<----------8<-----

Terminal output:
-----8<----------8<----------8<----------8<----------8<----------8<-----
$ gforth callback-hello.fs
including fflib.fs [ffcall]
foreign-test
callback-hello.fs:26:<3> 1073923411 2 2
into_callback 4002c553 2 2
foreign_plus 2 2
returning 4
4
callback-hello.fs:27:<0>
forth-test
callback-hello.fs:31:<3> 134778208 2 2
into_callback 8088d60 2 2

in file included from *OS command line*:-1
callback-hello.fs:35: Invalid memory address
>>>forth-test<<<

Backtrace:
$ echo $?
1
-----8<----------8<----------8<----------8<----------8<----------8<-----

Expected behaviour: execute forth-plus and return to the forth-test
word without crashing.
Reply With Quote
  #2  
Old 09-05-2008, 05:37 PM
ddaa
Guest
 
Default Re: Callbacks in gforth-0.6.9-20080716

On Aug 30, 11:56*pm, ddaa <david.allou...@gmail.com> wrote:
> Hello, I have trouble making callbacks work in gforth-0.6.9-20080716.
>
> So far, I can use libcc to build both the callback and the caller
> function, and use them in gforth. But I cannot make the final step to
> lib.fs callbacks. Apparently, I'm not passing the callback right to
> the C code.


I had some time again to investigate. I think I tracked down the
problem to the way fflib calls back into gforth_engine.

At this point I'm stumped.

-----8<----------8<----------8<----------8<----------8<----------8<-----
\c #include <stdio.h>
\c #include <callback.h>
\c static va_alist gforth_clist;
\c static Cell *gforth_RP;
\c static char *gforth_LP;
\c typedef void *Label;
\c typedef Label *Xt;
\c Label *gforth_engine(Xt *ip, Cell *sp, Cell *rp0, Float *fp, char
*lp);
\c
\c void gforth_callback_ffcall2(Xt* fcall, void * alist)
\c {
\c printf("gforth_callback_ffcall2 %x %x\n", fcall, alist);
\c /* save global valiables */
\c Cell *rp = gforth_RP;
\c Cell *sp = gforth_SP;
\c Float *fp = gforth_FP;
\c char *lp = gforth_LP;
\c va_alist clist = gforth_clist;
\c
\c gforth_clist = (va_alist)alist;
\c
\c printf("calling back into gforth_engine\n");
\c gforth_engine(fcall, sp, rp, fp, lp);
\c printf("returned from gforth_engine\n");
\c
\c /* restore global variables */
\c gforth_RP = rp;
\c gforth_SP = sp;
\c gforth_FP = fp;
\c gforth_LP = lp;
\c gforth_clist = clist;
\c }
c-function gforth-callback-ffcall2 gforth_callback_ffcall2 a a -- void

: forth-void ( -- )
cr ." In forth-void" ;

: forth-engine-test
." forth-engine-test" cr
['] forth-void 0 gforth-callback-ffcall2 ;

forth-engine-test
bye
-----8<----------8<----------8<----------8<----------8<----------8<-----

Terminal output:
-----8<----------8<----------8<----------8<----------8<----------8<-----
-*- mode: compilation; default-directory: "~/Documents/Forth/" -*-
Compilation started at Fri Sep 5 23:06:57

gforth callback-debug.fs
forth-engine-test
gforth_callback_ffcall2 401f0510 0
calling back into gforth_engine

in file included from *OS command line*:-1
callback-debug.fs:42: Invalid memory address
>>>forth-engine-test<<<

Backtrace:

Compilation exited abnormally with code 1 at Fri Sep 5 23:06:58
-----8<----------8<----------8<----------8<----------8<----------8<-----

Expected behaviour: at least printing "In forth-void" before crashing.
Preferably, not crash at all.
Reply With Quote
  #3  
Old 09-18-2008, 04:20 PM
Anton Ertl
Guest
 
Default Re: Callbacks in gforth-0.6.9-20080716

ddaa <david.allouche@gmail.com> writes:
>Hello, I have trouble making callbacks work in gforth-0.6.9-20080716.
>
>So far, I can use libcc to build both the callback and the caller
>function, and use them in gforth. But I cannot make the final step to
>lib.fs callbacks. Apparently, I'm not passing the callback right to
>the C code.
>
>The system is an Ubuntu 8.04, with my self-made package of gforth, out
>of combining the existing package and the gforth-0.6.9-20080716
>upstream ("make check" passes). By the way, I'd be glad to give it to
>anyone who could help make it land in Debian.
>
>Can anyone (yes, Anton, I'm looking at you) help me make it work?


Actually, Bernd is the guy who knows about the callbacks (and I was
busy). And with the latest changes he has just committed, this
example seems to work nicely, whether I use libffi.fs or fflib.fs.
This is on a Linux-AMD64 box:

foreign-test
xxxx.fs:26:<3> 46912513935352 2 2
into_callback abba87f8 2 2
foreign_plus 2 2
returning 4
4
xxxx.fs:27:<0>
forth-test
xxxx.fs:31:<3> 5944960 2 2
into_callback 5ab680 2 2

Testing callback
arguments: <5> 5944960 2 2 2 2
result <4> 5944960 2 2 4
returning 4
4
xxxx.fs:32:<0>

I did not test your code from the other posting (my newsreader does
not decode quoted-printable).

- anton
--
M. Anton Ertl http://www.complang.tuwien.ac.at/anton/home.html
comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
New standard: http://www.forth200x.org/forth200x.html
EuroForth 2008: http://www.euroforth.org/ef08.html
Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 03:22 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.