[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: another problem interfacing prolog and C

From: Daniel Diaz
Subject: Re: another problem interfacing prolog and C
Date: Wed, 15 Dec 2004 15:44:44 +0100
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.3) Gecko/20040913

Hello Erick,

the problem comes from the fact you invoke Prolog from your call_c/2 foreign code. This one uses 2 arguments (a and _L) which are stored into an array (accessible via macros X(0) and X(1)). But since write/1 itself is partly written in Prolog it also uses X(...) variables. So, the clean way to fix it would be to inform the compiler a foreign code calls Prolog (and thus the compiler would emit code to save/restore the X(...) vars used).

For the moment you can do it by hand (okay it is a bit dirty) as follows:

#include "gprolog.h"
calling_c(PlTerm In,PlTerm* Out)
  PlTerm arg[2];        /* reserve space to save args */
  int i;

  for(i=0;i<2;i++)   /* save the 2 args */


  for(i=0;i<2;i++)   /* restore them */

  *Out = Mk_Atom(atom_nil);

  return TRUE;  /* BTW: I prefer return TRUE; rather return PL_SUCCESS*/

In fact only X(1) (corresponding to _L) should be save since, at the exit its result is unified (with _L) while X(0) (a) is no longer used.

Sorry for this limitation.

Erick Alphonse wrote:

Still playing with the interface, I would like to call C from prolog and then from this C function call prolog back. Unfortunately, I can't get it working.

:- initialization(top).

:- foreign(call_c(+term,-term)).

top :-

% examp_c.c
#include <string.h>
#include "gprolog.h"

call_c(PlTerm In,PlTerm* Out)

  *Out = Mk_Atom(atom_nil);

  return PL_SUCCESS;

And I get :
874# gplc examp_c.c
875# ./examp
warning: /home/alphonse/tmp/bug-prolog/ user directive failed
GNU Prolog 1.2.18
By Daniel Diaz
Copyright (C) 1999-2004 Daniel Diaz
| ?-

That is, the variable _L in top/0 cannot be unified at the exit of call_c, like if the inner query was messing with the stack. If I comment the inner query, it works of course. Could someone help me out and tell me if, first, what I try to do is possible with the gprolog interface and how to do it properly? I can't figure it out myself as I guess I exhausted all the examples in the doc.

Thanks in advance for your help, hopefully I could get back to work.


Users-prolog mailing list

                 Daniel Diaz
University of Paris 1      INRIA Rocquencourt
75013 Paris FRANCE      78153 Le Chesnay FRANCE
        email: address@hidden

Ce message a été vérifié par MailScanner
pour des virus ou des polluriels et rien de
suspect n'a été trouvé.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]