Is there any tool that can automatically generate Prolog callable interface from C functions?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Is there any tool that can automatically generate Prolog callable interface from C functions?

Shuai Wang

So basically I am using Prolog to call a large number of C functions using GNU-Prolog.

Here is an example:

int add(int a, int b)
{
int res = a + b;

return res;
}

If I want to call this function from Prolog code, I need to create this interface:

#include <gprolog.h>

 PlBool add_interface(int a, int b, int* r)
{
  *r = add(a, b);
}

And In the Prolog code, I need add this :

 :- foreign(add(+integer, +integer, -integer)).

So I am dealing with a large number of C functions, and manually create these interface one by one would be quite time consuming...

Of course I can write an ad-hoc tool to do this creation(Using CIL probably..), but I just don't want to reinvent the wheel.

So my question is :

Is there any tool that can automatically generate Prolog callable interface from C functions?



_______________________________________________
Users-prolog mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/users-prolog
Reply | Threaded
Open this post in threaded view
|

Re: Is there any tool that can automatically generate Prolog callable interface from C functions?

emacstheviking
Shuai,

I too wanted something like this a few months back as I was attempting to bind OpenGL with gprolog. Perhaps it might be easier to embed gprolog within the thing you are attempting to call instead, so for me, instead of creating an OpenGL binding I embedded gprolog within a C program that initialised the framework..


And be the first to do it..that would be useful indeed!

My only idea would be to use the GNU compiler options to output the parse tree as an XML file and then use XSLT to do something clever like generating the interface definitions… I have heard that the gnu output is hard to deal with though..

All thebest,
Sean Charles


On 11 Apr 2014, at 16:14, Shuai Wang <[hidden email]> wrote:

So basically I am using Prolog to call a large number of C functions using GNU-Prolog.

Here is an example:

int add(int a, int b)
{
int res = a + b;

return res;
}

If I want to call this function from Prolog code, I need to create this interface:

#include <gprolog.h>

 PlBool add_interface(int a, int b, int* r)
{
  *r = add(a, b);
}

And In the Prolog code, I need add this :

 :- foreign(add(+integer, +integer, -integer)).

So I am dealing with a large number of C functions, and manually create these interface one by one would be quite time consuming...

Of course I can write an ad-hoc tool to do this creation(Using CIL probably..), but I just don't want to reinvent the wheel.

So my question is :

Is there any tool that can automatically generate Prolog callable interface from C functions?


_______________________________________________
Users-prolog mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/users-prolog


_______________________________________________
Users-prolog mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/users-prolog
Reply | Threaded
Open this post in threaded view
|

Re: Is there any tool that can automatically generate Prolog callable interface from C functions?

Shuai Wang
Hi Sean,

Thank you for your reply. 
So basically as I am writing some extensions for CIL(C Intermediate Language),
I quickly wrote some code based on our original extension to do the transform.

Currently it can work on the example I give and automatically generate
add_interface function in C and foreign interface in Prolog. 

However, I should point out that when dealing with some "trivial" cases, it is easy to do,
but when C pointers involved (such as pointers as arguments of C function ), things could become quite difficult.
I post some questions Q1 Q2 Q3 on stackoverflow about the pointer issues.

Generally speaking, I think some C static analysis tools could be useful for this topic, such as CIL or Clang.

Best,
Shuai



On Sun, Apr 13, 2014 at 4:23 PM, Sean Charles <[hidden email]> wrote:
Shuai,

I too wanted something like this a few months back as I was attempting to bind OpenGL with gprolog. Perhaps it might be easier to embed gprolog within the thing you are attempting to call instead, so for me, instead of creating an OpenGL binding I embedded gprolog within a C program that initialised the framework..


And be the first to do it..that would be useful indeed!

My only idea would be to use the GNU compiler options to output the parse tree as an XML file and then use XSLT to do something clever like generating the interface definitions… I have heard that the gnu output is hard to deal with though..

All thebest,
Sean Charles


On 11 Apr 2014, at 16:14, Shuai Wang <[hidden email]> wrote:

So basically I am using Prolog to call a large number of C functions using GNU-Prolog.

Here is an example:

int add(int a, int b)
{
int res = a + b;

return res;
}

If I want to call this function from Prolog code, I need to create this interface:

#include <gprolog.h>

 PlBool add_interface(int a, int b, int* r)
{
  *r = add(a, b);
}

And In the Prolog code, I need add this :

 :- foreign(add(+integer, +integer, -integer)).

So I am dealing with a large number of C functions, and manually create these interface one by one would be quite time consuming...

Of course I can write an ad-hoc tool to do this creation(Using CIL probably..), but I just don't want to reinvent the wheel.

So my question is :

Is there any tool that can automatically generate Prolog callable interface from C functions?


_______________________________________________
Users-prolog mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/users-prolog



_______________________________________________
Users-prolog mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/users-prolog
Reply | Threaded
Open this post in threaded view
|

Re: Is there any tool that can automatically generate Prolog callable interface from C functions?

Shuai Wang
BTW: I don't know if I am on the right way( particular on how to handle the pointers), 
and any suggestions are welcomed:)

Best,
Shuai

On Sun, Apr 13, 2014 at 5:36 PM, Shuai Wang <[hidden email]> wrote:
Hi Sean,

Thank you for your reply. 
So basically as I am writing some extensions for CIL(C Intermediate Language),
I quickly wrote some code based on our original extension to do the transform.

Currently it can work on the example I give and automatically generate
add_interface function in C and foreign interface in Prolog. 

However, I should point out that when dealing with some "trivial" cases, it is easy to do,
but when C pointers involved (such as pointers as arguments of C function ), things could become quite difficult.
I post some questions Q1 Q2 Q3 on stackoverflow about the pointer issues.

Generally speaking, I think some C static analysis tools could be useful for this topic, such as CIL or Clang.

Best,
Shuai



On Sun, Apr 13, 2014 at 4:23 PM, Sean Charles <[hidden email]> wrote:
Shuai,

I too wanted something like this a few months back as I was attempting to bind OpenGL with gprolog. Perhaps it might be easier to embed gprolog within the thing you are attempting to call instead, so for me, instead of creating an OpenGL binding I embedded gprolog within a C program that initialised the framework..


And be the first to do it..that would be useful indeed!

My only idea would be to use the GNU compiler options to output the parse tree as an XML file and then use XSLT to do something clever like generating the interface definitions… I have heard that the gnu output is hard to deal with though..

All thebest,
Sean Charles


On 11 Apr 2014, at 16:14, Shuai Wang <[hidden email]> wrote:

So basically I am using Prolog to call a large number of C functions using GNU-Prolog.

Here is an example:

int add(int a, int b)
{
int res = a + b;

return res;
}

If I want to call this function from Prolog code, I need to create this interface:

#include <gprolog.h>

 PlBool add_interface(int a, int b, int* r)
{
  *r = add(a, b);
}

And In the Prolog code, I need add this :

 :- foreign(add(+integer, +integer, -integer)).

So I am dealing with a large number of C functions, and manually create these interface one by one would be quite time consuming...

Of course I can write an ad-hoc tool to do this creation(Using CIL probably..), but I just don't want to reinvent the wheel.

So my question is :

Is there any tool that can automatically generate Prolog callable interface from C functions?


_______________________________________________
Users-prolog mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/users-prolog




_______________________________________________
Users-prolog mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/users-prolog