From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Stefan Israelsson Tampe Newsgroups: gmane.lisp.guile.devel Subject: Re: patch to guile to allow c-code closure interaction for logic programming Date: Tue, 22 May 2012 21:13:06 +0200 Message-ID: References: <87wr4dji1x.fsf@pobox.com> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary=14dae9340639fa5c7004c0a4ce49 X-Trace: dough.gmane.org 1337714004 25420 80.91.229.3 (22 May 2012 19:13:24 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Tue, 22 May 2012 19:13:24 +0000 (UTC) Cc: guile-devel To: Andy Wingo Original-X-From: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Tue May 22 21:13:22 2012 Return-path: Envelope-to: guile-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1SWuWF-0002n9-VS for guile-devel@m.gmane.org; Tue, 22 May 2012 21:13:20 +0200 Original-Received: from localhost ([::1]:58041 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SWuWF-00013S-EF for guile-devel@m.gmane.org; Tue, 22 May 2012 15:13:19 -0400 Original-Received: from eggs.gnu.org ([208.118.235.92]:55804) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SWuW7-00011d-4X for guile-devel@gnu.org; Tue, 22 May 2012 15:13:17 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1SWuW4-0002CA-ND for guile-devel@gnu.org; Tue, 22 May 2012 15:13:10 -0400 Original-Received: from mail-gg0-f169.google.com ([209.85.161.169]:58091) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SWuW4-0002C0-FJ for guile-devel@gnu.org; Tue, 22 May 2012 15:13:08 -0400 Original-Received: by ggm4 with SMTP id 4so6987265ggm.0 for ; Tue, 22 May 2012 12:13:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=xu5/CA7RRSHAPm/yYOGDWjNiW9JTTL4no3VvmsrTlRQ=; b=aKQoymJjOOxONw7xtQ8DQ4guobbp5/zqaIyuKwGzwo1gytu68+UXVV0ibJw3U9AW9d rfiXG4Ow9551y6Jy0YSOHWdXSILBDOAzKYHQ/wXtutTqgQUorehZYNBJKANO1jk+Lu/P OrBMNLPK8SW+14F9n991Wxy4ynwFu401M2O0MrAFK6GwAaowHx1/vvfjB6Os/SAqKQly r3N59wykzsPtDfUxCNzRHs9G9MjRgdv1y57ye0ybCopGHe+HJxgkuU89VnO2amDQWuLb DYkFSbH9zyq7rs/2PlBuda3uiDnpIdM5bkWoaGorF4OzheErkC4ExTAZtmwIMcIL8HR4 QxKw== Original-Received: by 10.50.51.132 with SMTP id k4mr10683404igo.17.1337713986526; Tue, 22 May 2012 12:13:06 -0700 (PDT) Original-Received: by 10.50.242.102 with HTTP; Tue, 22 May 2012 12:13:06 -0700 (PDT) In-Reply-To: <87wr4dji1x.fsf@pobox.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 209.85.161.169 X-BeenThere: guile-devel@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "Developers list for Guile, the GNU extensibility library" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Original-Sender: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.lisp.guile.devel:14504 Archived-At: --14dae9340639fa5c7004c0a4ce49 Content-Type: multipart/alternative; boundary=14dae9340639fa5c6c04c0a4ce47 --14dae9340639fa5c6c04c0a4ce47 Content-Type: text/plain; charset=ISO-8859-1 I use this method to interop c-code compiled logic programs o guile, note: I saw that there have been restructuring the call logic somewhat meaning that the dispatching to the logic c code get's a little more expensive and a little more different then presented below bot hey we can fix my code later on! Assume that we add a check for a data-structure representing a c code closure that is located last in the chain of tests for special callable structures just to be defensive to other systems who want to call! this check in call, tail-call and mv-call can then point to the different goto labels in the supplied file (let's forget about mv-call for now) Then the execution of the c-code will be done by starting a trampoline. This trampoline assumes c functions that takes an argument representing the call stack, the number of arguments, a pointer to the list of closure variables and optionally a pointer representing the end of the stack. the function assumes it return a integer. If it is possitive then there has been a new function and arguments loaded into the old functions possition e.g. an ordinary tail call from c land, the integer is the number of arguments for this tail call. If the integer is negative then a return has been done with the absolute value of the return representing the number of returned values - useful information for mv-call logic. Currently the c closure is represented as a cons of a tag with a vector consisting of an integerization of the function pointer and closure data. This should be changed later on but for now it's the right thing because of the transparancy. (later on a smob would be a better fit) By the way smob checks seems to be a little expensive, I noted that my logic code spend a lot of time in these smob tests and wonders if the tests for smob identity can be optimized? So the basic code for handling this logic is attached to this document. Coding c code using this method is error prone and perhaps not that useful as an official interface to C-land. On the other hand I have a macro system in scheme that transform a mix of scheme and C inspired language out to C and using this system the interfacing is automated (I can actually write logic programming code and it compiled it directly to C). It's just a proof of concept right now and is a way to test the limits of the logic framework (guile-log) I've coded on for some time. So Comments lpease there might be some details I'm missing and also iI wouldlike to have comments if it would be possible to include such a system and maybe put it in relation to how we intend to use naitive code in the future! On the other hand, apart from the bloat, the wished for changes to the code base is not that intrusive so we could as well implement this so that people can play with the logic system even on the c-level without needing to recompile a patched guile! /Stefan On Tue, May 15, 2012 at 10:38 PM, Andy Wingo wrote: > On Wed 09 May 2012 18:07, Stefan Israelsson Tampe > writes: > > > I post here a directory of patches and code that can be used to enhance > guile to interact nicely > > with logic programming code written in C. I can reach very good > performance using this tool > > please have a look and comment on it. > > Would you mind attaching them to the message directly? It's easier to > read and comment that way. > > If you want, you can do the linux kernel thing of one patch per > message. But as you like. > > Cheers, > > Andy > -- > http://wingolog.org/ > --14dae9340639fa5c6c04c0a4ce47 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable I use this method to interop c-code compiled logic programs o guile,
note: I saw that there have been restructuring the call logic somewhat mea= ning that
the dispatching to the logic c code get's a little more ex= pensive and a little more different then presented below bot hey we can fix= my code later on!

Assume that we add a check for a data-structure representing a c code c= losure that is located last in the chain of tests for special callable stru= ctures just to be defensive to other
systems who want to call!

this check in call, tail-call and mv-call can then point to the different g= oto labels in the supplied file (let's forget about mv-call for now) Th= en the execution of the c-code will be done by starting a trampoline. This = trampoline assumes c functions that takes an argument representing the call= stack, the number of arguments, a pointer to the list of closure variables= and optionally a pointer representing the end of the stack. the function a= ssumes it return a integer. If it is possitive then there has been a new fu= nction and arguments loaded into the old functions possition e.g. an ordina= ry tail call from c land, the integer is the number of arguments for this t= ail call. If the integer is negative then a return has been done with the a= bsolute value of the return representing the number of returned values - us= eful information for mv-call logic.

Currently the c closure is represented as a cons of a tag with a vector= consisting of an integerization of the function pointer and closure data. = This should be changed later on but for now it's the right thing becaus= e of the transparancy. (later on a smob would be a better fit)

By the way smob checks seems to be a little expensive, I noted that my = logic code spend a lot of time in these smob tests and wonders if the tests= for smob identity can be optimized?

So the basic code for handling = this logic is attached to this document.

Coding c code using this method is error prone and perhaps not that use= ful as an official interface to C-land. On the other hand I have a macro sy= stem in scheme that transform a mix of scheme and C inspired language out t= o C and using this system the interfacing is automated (I can actually writ= e logic programming code and it compiled it directly to C). It's just a= proof of concept right now and is a way to test the limits of the logic fr= amework (guile-log) I've coded on for some time.

So Comments lpease there might be some details I'm missing and also= iI wouldlike to have comments if it would be possible to include such a sy= stem and maybe put it in relation to how we intend to use naitive code in t= he future! On the other hand, apart from the bloat, the wished for changes = to the code base is not that intrusive so we could as well implement this so that people can play with the logic system even on the c-level without n= eeding to recompile a patched guile!

/Stefan





=
On Tue, May 15, 2012 at 10:38 PM, Andy Wingo= <wingo@pobox.com> wrote:
On Wed 09 May 2012 18:07, = Stefan Israelsson Tampe <stef= an.itampe@gmail.com> writes:

> I post here a directory of patches and code that can be used to enhanc= e guile to interact nicely
> with logic programming code written in C. I can reach very good perfor= mance using this tool
> please have a look and comment on it.

Would you mind attaching them to the message directly? =A0It's ea= sier to
read and comment that way.

If you want, you can do the linux kernel thing of one patch per
message. =A0But as you like.

Cheers,

Andy
--
http://wingolog.org/=

--14dae9340639fa5c6c04c0a4ce47-- --14dae9340639fa5c7004c0a4ce49 Content-Type: text/x-csrc; charset=US-ASCII; name="vm-i-subr.c" Content-Disposition: attachment; filename="vm-i-subr.c" Content-Transfer-Encoding: base64 X-Attachment-Id: f_h2jc9scm0 I2RlZmluZSBOVU0yUFRSKHgpICgoU0NNICopIChTQ01fVU5QQUNLKHgpICYgfjIpKQp7CiAgLy9D IHRyYW1wb2xpbmVzCiAgaW50IHJldDsgU0NNICpzcHA7CiAgY29uc3QgU0NNICp2ZWM7CiAgaW50 ICgqc3VicikoU0NNICoqc3BwLCBpbnQgbmFyZ3MsIGNvbnN0IFNDTSAqY2xvc3VyZSwgY29uc3Qg U0NNICplbmQpOwoKCiAgCiB2bV9jX2NhbGw6CgogIC8vcHJpbnRmKCJjIGNhbGwhXG4iKTsKCiAg dmVjICA9IFNDTV9JX1ZFQ1RPUl9FTFRTKFNDTV9DRFIocHJvZ3JhbSkpOwoKICAKICAvL1ZNX0hB TkRMRV9JTlRFUlJVUFRTOwogIC8vU1lOQ19SRUdJU1RFUiAoKTsKICAKICBzcHAgID0gIHNwOwog IFNDTSAqc3Bfb2xkID0gc3A7CiAgLy9wcmludGYoImRwICVwXG4iLHNwKTsKICBzdWJyID0gKHN1 YnJfdHlwZSkgTlVNMlBUUih2ZWNbMF0pOwogIHJldCAgPSBzdWJyKCZzcHAsbmFyZ3MsdmVjICsg MSxzdGFja19saW1pdCk7CgogIC8vQ0FDSEVfUkVHSVNURVIgKCk7ICAKCiAgc3AgICA9IHNwcDsK ICBpZihyZXQ8MCkgCiAgICB7CiAgICAgIC8vcHJpbnRmKCJyZXR1cm4gY2FsbFxuIik7CiAgICAg IHNwID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