Skip to content
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

[Feature Request]: Handle more C types in FriCAS FFI macros (fricas-lisp.lisp) #157

Open
gvanuxem opened this issue Mar 4, 2024 · 1 comment

Comments

@gvanuxem
Copy link

gvanuxem commented Mar 4, 2024

Hello,

I hesitated to add this as an issue at GitHub.com (feature request), I
put it here first.

FriCAS supports what I would call preliminary support of FFI stuff via
a set of CL macros. I would like to suggest adding more types in this
regard. For example, it could be possible to add double-float arrays,
boolean and the like. I modified, and well tested, the following for
example. For information, only for SBCL and Clozure CL, but adding
those additional types for other CL implementations should be easy I
think:

diff --git a/home/greg/Tmp/fricas/src/lisp/fricas-lisp.lisp
b/src/lisp/fricas-lisp.lisp
index 357aa063..8de10db7 100644
--- a/home/greg/Tmp/fricas/src/lisp/fricas-lisp.lisp
+++ b/src/lisp/fricas-lisp.lisp
@@ -363,10 +363,13 @@ with this hack and will try to convince the GCL
crowd to fix this.
 )

 (defvar *c_type_as_string* '(
+    (void "void")
+    (bool "bool")
     (int "int")
     (c-string "char *")
     (double "double")
     (char-* "char *")
+    (double-* "double *")
 ))

 (defun c_type_as_string(c_type) (nth 1 (assoc c_type *c_type_as_string*)))
@@ -465,10 +468,13 @@ with this hack and will try to convince the GCL
crowd to fix this.
 (eval-when (:compile-toplevel :load-toplevel :execute)

 (setf *c-type-to-ffi* '(
-    (int SB-ALIEN::int)
-    (c-string SB-ALIEN::c-string)
-    (double SB-ALIEN::double)
-    (char-* (sb-alien:* sb-alien:char))
+    (void    sb-alien::void)
+    (bool    (sb-alien::boolean 8))
+    (int      sb-alien::int)
+    (c-string (sb-alien::c-string))
+    (double   sb-alien::double-float)
+    (char-*   (sb-alien:* sb-alien:char))
+    (double-*   (sb-alien:* sb-alien:double-float))
 ))

 (defun c-args-to-sbcl (arguments)
@@ -490,10 +496,13 @@ with this hack and will try to convince the GCL
crowd to fix this.
 (eval-when (:compile-toplevel :load-toplevel :execute)

 (setf *c-type-to-ffi* '(
+    (void :void)
+    (bool :signed-byte)
     (int :int)
     (c-string :address)
     (double :double-float)
     (char-* :address)
+    (double-* :address)
 ))

 (defun c-args-to-openmcl (arguments)
@@ -518,7 +527,7 @@ with this hack and will try to convince the GCL
crowd to fix this.
                  `(ccl::external-call ,c-name ,@fargs ,l-ret))
                (fun-body
                   (if strs
-                     `(ccl::with-cstrs ,strs ,call-body)
+                     `(ccl::with-encoded-cstrs :utf-8  ,strs ,call-body)
                       call-body)))
                `(defun ,name ,largs ,fun-body))))

@@ -532,7 +541,7 @@ with this hack and will try to convince the GCL
crowd to fix this.

 (setf *c-type-to-ffi* '(
                  (int :int)
-                 (c-string  :cstring )
+                 (c-string  :cstring)
                  (double :double)
                  (char-* :pointer-void)

The patch file is attached. One remark, I added UTF-8 to Clozure CL
encoded-cstrs in such a way some non-ascii characters can be used in
the Spad source code, SBCL has as default external format UTF-8. It
can be modified via sb-ext:default-external-format and
sb-ext:default-c-string-external-format. This is not an intent to
fully support UTF-8 in Spad code, it just allows, for example, to use:

    x quo y == ibinfunc("÷", x, y)
instead of (commented here)
    -- x quo y == ibinfunc("div", x, y)
x rem y == ibinfunc("%", x, y)

where ibinfunc is a macro that expands binary functions operating on integers.

Regards,

Greg

fricas.patch

@gvanuxem
Copy link
Author

gvanuxem commented Mar 4, 2024

More information from the fricas-devel mailing list:

https://groups.google.com/g/fricas-devel/c/yqSG-2Vs8N4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant