1 (in-package :SB-GROVEL)
2 (defvar *export-symbols* nil)
4 (defun c-for-structure (stream lisp-name c-struct)
5 (destructuring-bind (c-name &rest elements) c-struct
6 (format stream "printf(\"(sb-grovel::define-c-struct ~A %d)\\n\",sizeof (~A));~%" lisp-name c-name)
8 (destructuring-bind (lisp-type lisp-el-name c-type c-el-name &key distrust-length) e
9 ;; FIXME: this format string doesn't actually guarantee
10 ;; non-multilined-string-constantness, it just makes it more
11 ;; likely. Sort out the required behaviour (and maybe make
12 ;; the generated C more readable, while we're at it...) --
14 (format stream "printf(\"(sb-grovel::define-c-accessor ~A-~A\\n\\~% ~
16 lisp-name lisp-el-name lisp-name lisp-type)
18 (format stream "{ ~A t;printf(\"%d \",((unsigned long)&(t.~A)) - ((unsigned long)&(t)) ); }~%"
22 (format stream "printf(\"nil\");")
23 (format stream "{ ~A t;printf(\"%d\",(sizeof t.~A));}~%"
25 (format stream "printf(\")\\n\");~%")))))
27 (defun c-for-function (stream lisp-name alien-defn)
28 (destructuring-bind (c-name &rest definition) alien-defn
29 (format stream "printf(\"(cl:declaim (cl:inline ~A))\\n\");~%" lisp-name)
31 "printf(\"(sb-grovel::define-foreign-routine (\\\"~A\\\" ~A)\\n\\~%~
32 ~{ ~W~^\\n\\~%~})\\n\");~%"
33 c-name lisp-name definition)))
35 (defun print-c-source (stream headers definitions package-name)
36 (let ((*print-right-margin* nil))
37 (format stream "#define SIGNEDP(x) (((x)-1)<0)~%")
38 (format stream "#define SIGNED_(x) (SIGNEDP(x)?\"\":\"un\")~%")
39 (loop for i in (cons "stdio.h" headers)
40 do (format stream "#include <~A>~%" i))
41 (format stream "main() { ~%
42 printf(\"(in-package ~S)\\\n\");~%" package-name)
43 (format stream "printf(\"(cl:deftype int () '(%ssigned-byte %d))\\\n\",SIGNED_(int),8*sizeof (int));~%")
44 (format stream "printf(\"(cl:deftype char () '(%ssigned-byte %d))\\\n\",SIGNED_(char),8*sizeof (char));~%")
45 (format stream "printf(\"(cl:deftype long () '(%ssigned-byte %d))\\\n\",SIGNED_(long),8*sizeof (long));~%")
46 (format stream "printf(\"(cl:defconstant size-of-int %d)\\\n\",sizeof (int));~%")
47 (format stream "printf(\"(cl:defconstant size-of-char %d)\\\n\",sizeof (char));~%")
48 (format stream "printf(\"(cl:defconstant size-of-long %d)\\\n\",sizeof (long));~%")
49 (dolist (def definitions)
50 (destructuring-bind (type lispname cname &optional doc) def
51 (cond ((eq type :integer)
53 "printf(\"(cl:defconstant ~A %d \\\"~A\\\")\\\n\",~A);~%"
57 "printf(\"(sb-alien:define-alien-type ~A (sb-alien:%ssigned %d))\\\n\",SIGNED_(~A),8*(sizeof(~A)));~%"
58 lispname cname cname))
61 "printf(\"(cl:defvar ~A %S \\\"~A\\\")\\\n\",~A);~%"
64 (c-for-function stream lispname cname))
66 (c-for-structure stream lispname cname))
69 "printf(\";; Non hablo Espagnol, Monsieur~%")))))
70 (format stream "exit(0);~%}~%")))
72 (defun c-constants-extract (filename output-file package)
73 (with-open-file (f output-file :direction :output)
74 (with-open-file (i filename :direction :input)
75 (let* ((headers (read i))
76 (definitions (read i)))
77 (print-c-source f headers definitions package)))))
79 (defclass grovel-constants-file (asdf:cl-source-file)
80 ((package :accessor constants-package :initarg :package)))
82 (defmethod asdf:perform ((op asdf:compile-op)
83 (component grovel-constants-file))
84 ;; we want to generate all our temporary files in the fasl directory
85 ;; because that's where we have write permission. Can't use /tmp;
86 ;; it's insecure (these files will later be owned by root)
87 (let* ((output-file (car (output-files op component)))
88 (filename (component-pathname component))
90 (if (typep output-file 'logical-pathname)
91 (translate-logical-pathname output-file)
92 (pathname output-file)))
93 (tmp-c-source (merge-pathnames #p"foo.c" real-output-file))
94 (tmp-a-dot-out (merge-pathnames #p"a.out" real-output-file))
95 (tmp-constants (merge-pathnames #p"constants.lisp-temp"
97 (princ (list filename output-file real-output-file
98 tmp-c-source tmp-a-dot-out tmp-constants))
100 (funcall (intern "C-CONSTANTS-EXTRACT" (find-package "SB-GROVEL"))
101 filename tmp-c-source (constants-package component))
103 (= (run-shell-command "gcc -o ~S ~S" (namestring tmp-a-dot-out)
104 (namestring tmp-c-source)) 0)
105 (= (run-shell-command "~A >~A"
106 (namestring tmp-a-dot-out)
107 (namestring tmp-constants)) 0)
108 (compile-file tmp-constants :output-file output-file))))