c5a6241dec6b215d3716404e733e9d174e070555
[sbcl.git] / contrib / sb-grovel / def-to-lisp.lisp
1 (in-package #:sb-grovel)
2
3 (defvar *default-c-stream* nil)
4
5 (defun escape-for-string (string)
6   (c-escape string))
7
8 (defun split-cflags (string)
9   (remove-if (lambda (flag)
10                (zerop (length flag)))
11              (loop
12                 for start = 0 then (if end (1+ end) nil)
13                 for end = (and start (position #\Space string :start start))
14                 while start
15                 collect (subseq string start end))))
16
17 (defun c-escape (string &optional (dangerous-chars '(#\")) (escape-char #\\))
18   "Escape DANGEROUS-CHARS in STRING, with ESCAPE-CHAR."
19   (coerce (loop for c across string
20                 if (member c dangerous-chars) collect escape-char
21                 collect c)
22           'string))
23
24 (defun as-c (&rest args)
25   "Pretty-print ARGS into the C source file, separated by #\Space"
26   (format *default-c-stream* "~A~{ ~A~}~%" (first args) (rest args)))
27
28 (defun printf (formatter &rest args)
29   "Emit C code to fprintf the quoted code, via FORMAT.
30 The first argument is the C string that should be passed to
31 printf.
32
33 The rest of the arguments are consumed by FORMAT clauses, until
34 there are no more FORMAT clauses to fill. If there are more
35 arguments, they are emitted as printf arguments.
36
37 There is no error checking done, unless you pass too few FORMAT
38 clause args. I recommend using this formatting convention in
39 code:
40
41  (printf \"string ~A ~S %d %d\" format-arg-1 format-arg-2
42          printf-arg-1 printf-arg-2)"
43   (let ((*print-pretty* nil))
44     (apply #'format *default-c-stream*
45            "    fprintf (out, \"~@?\\n\"~@{, ~A~});~%"
46            (c-escape formatter)
47            args)))
48
49 (defun c-for-enum (lispname elements export)
50   (printf "(cl:eval-when (:compile-toplevel :load-toplevel :execute) (sb-alien:define-alien-type ~A (sb-alien:enum nil" lispname)
51   (dolist (element elements)
52     (destructuring-bind (lisp-element-name c-element-name) element
53       (printf " (~S %d)" lisp-element-name c-element-name)))
54   (printf ")))")
55   (when export
56     (dolist (element elements)
57       (destructuring-bind (lisp-element-name c-element-name) element
58         (declare (ignore c-element-name))
59         (unless (keywordp lisp-element-name)
60           (printf "(export '~S)" lisp-element-name))))))
61
62 (defun c-for-structure (lispname cstruct)
63   (destructuring-bind (cname &rest elements) cstruct
64     (printf "(cl:eval-when (:compile-toplevel :load-toplevel :execute) (sb-grovel::define-c-struct ~A %d" lispname
65             (format nil "sizeof(~A)" cname))
66     (dolist (e elements)
67       (destructuring-bind (lisp-type lisp-el-name c-type c-el-name &key distrust-length) e
68         (printf " (~A ~A \"~A\"" lisp-el-name lisp-type c-type)
69         ;; offset
70         (as-c "{" cname "t;")
71         (printf "  %d"
72                 (format nil "((unsigned long)&(t.~A)) - ((unsigned long)&(t))" c-el-name))
73         (as-c "}")
74         ;; length
75         (if distrust-length
76             (printf "  0)")
77             (progn
78               (as-c "{" cname "t;")
79               (printf "  %d)"
80                       (format nil "sizeof(t.~A)" c-el-name))
81               (as-c "}")))))
82     (printf "))")))
83
84 (defun print-c-source (stream headers definitions package-name)
85   (declare (ignorable definitions package-name))
86   (let ((*default-c-stream* stream)
87         (*print-right-margin* nil))
88     (loop for i in (cons "stdio.h" headers)
89           do (format stream "#include <~A>~%" i))
90     (as-c "#define SIGNEDP(x) (((x)-1)<0)")
91     (as-c "#define SIGNED_(x) (SIGNEDP(x)?\"\":\"un\")")
92     (as-c "int main(int argc, char *argv[]) {")
93     (as-c "    FILE *out;")
94     (as-c "    if (argc != 2) {")
95     (as-c "        printf(\"Invalid argcount!\");")
96     (as-c "        return 1;")
97     (as-c "    } else")
98     (as-c "        out = fopen(argv[1], \"w\");")
99     (as-c "    if (!out) {")
100     (as-c "        printf(\"Error opening output file!\");")
101     (as-c "        return 1;")
102     (as-c "    }")
103     (printf "(cl:in-package #:~A)" package-name)
104     (printf "(cl:eval-when (:compile-toplevel)")
105     (printf "  (cl:defparameter *integer-sizes* (cl:make-hash-table))")
106     (dolist (type '("char" "short" "long" "int"
107                     #+nil"long long" ; TODO: doesn't exist in sb-alien yet
108                     ))
109       (printf "  (cl:setf (cl:gethash %d *integer-sizes*) 'sb-alien:~A)" (substitute #\- #\Space type)
110               (format nil "sizeof(~A)" type)))
111     (printf ")")
112     (dolist (def definitions)
113       (destructuring-bind (type lispname cname &optional doc export) def
114         (case type
115           (:integer
116            (as-c "#ifdef" cname)
117            (printf "(cl:defconstant ~A %d \"~A\")" lispname doc
118                    cname)
119            (as-c "#else")
120            (printf "(sb-int:style-warn \"Couldn't grovel for ~A (unknown to the C compiler).\")" cname)
121            (as-c "#endif"))
122           (:enum
123            (c-for-enum lispname cname export))
124           (:type
125            (printf "(cl:eval-when (:compile-toplevel :load-toplevel :execute) (sb-alien:define-alien-type ~A (sb-alien:%ssigned %d)))" lispname
126                    (format nil "SIGNED_(~A)" cname)
127                    (format nil "(8*sizeof(~A))" cname)))
128           (:string
129            (printf "(cl:defparameter ~A %s \"~A\"" lispname doc
130                    cname))
131           (:function
132            (printf "(cl:declaim (cl:inline ~A))" lispname)
133            (destructuring-bind (f-cname &rest definition) cname
134              (printf "(sb-grovel::define-foreign-routine (\"~A\" ~A)" f-cname lispname)
135              (printf "~{  ~W~^\\n~})" definition)))
136           (:structure
137            (c-for-structure lispname cname))
138           (otherwise
139            ;; should we really not sprechen espagnol, monsieurs?
140            (error "Unknown grovel keyword encountered: ~A" type)))
141         (when export
142           (printf "(cl:export '~A)" lispname))))
143     (as-c "return 0;")
144     (as-c "}")))
145
146 (defun c-constants-extract  (filename output-file package)
147   (with-open-file (f output-file :direction :output :if-exists :supersede)
148     (with-open-file (i filename :direction :input)
149       (let* ((headers (read i))
150              (definitions (read i)))
151         (print-c-source  f headers definitions package)))))
152
153 (defclass grovel-constants-file (asdf:cl-source-file)
154   ((package :accessor constants-package :initarg :package)))
155
156 (define-condition c-compile-failed (compile-failed) ()
157   (:report (lambda (c s)
158              (format s "~@<C compiler failed when performing ~A on ~A.~@:>"
159                      (error-operation c) (error-component c)))))
160 (define-condition a-dot-out-failed (compile-failed) ()
161   (:report (lambda (c s)
162              (format s "~@<a.out failed when performing ~A on ~A.~@:>"
163                      (error-operation c) (error-component c)))))
164
165 (defmethod asdf:perform ((op asdf:compile-op)
166                          (component grovel-constants-file))
167   ;; we want to generate all our temporary files in the fasl directory
168   ;; because that's where we have write permission.  Can't use /tmp;
169   ;; it's insecure (these files will later be owned by root)
170   (let* ((output-file (car (output-files op component)))
171          (filename (component-pathname component))
172          (real-output-file
173           (if (typep output-file 'logical-pathname)
174               (translate-logical-pathname output-file)
175               (pathname output-file)))
176          (tmp-c-source (merge-pathnames #p"foo.c" real-output-file))
177          (tmp-a-dot-out (merge-pathnames #-win32 #p"a.out" #+win32 #p"a.exe"
178                                          real-output-file))
179          (tmp-constants (merge-pathnames #p"constants.lisp-temp"
180                                          real-output-file)))
181     (princ (list filename output-file real-output-file
182                  tmp-c-source tmp-a-dot-out tmp-constants))
183     (terpri)
184     (funcall (intern "C-CONSTANTS-EXTRACT" (find-package "SB-GROVEL"))
185              filename tmp-c-source (constants-package component))
186     (let ((code (sb-ext:process-exit-code
187                  (sb-ext:run-program
188                   (sb-ext:posix-getenv "CC")
189                   (append
190                    (split-cflags (sb-ext:posix-getenv "EXTRA_CFLAGS"))
191                    #+(and linux largefile)
192                    '("-D_LARGEFILE_SOURCE"
193                      "-D_LARGEFILE64_SOURCE"
194                      "-D_FILE_OFFSET_BITS=64")
195                    (list "-o"
196                          (namestring tmp-a-dot-out)
197                          (namestring tmp-c-source)))
198                   :search t
199                   :input nil
200                   :output *trace-output*))))
201       (unless (= code 0)
202         (case (operation-on-failure op)
203           (:warn (warn "~@<C compiler failure when performing ~A on ~A.~@:>"
204                        op component))
205           (:error
206            (error 'c-compile-failed :operation op :component component)))))
207     (let ((code (sb-ext:process-exit-code
208                  (sb-ext:run-program (namestring tmp-a-dot-out)
209                                      (list (namestring tmp-constants))
210                                      :search nil
211                                      :input nil
212                                      :output *trace-output*))))
213       (unless (= code 0)
214         (case (operation-on-failure op)
215           (:warn (warn "~@<a.out failure when performing ~A on ~A.~@:>"
216                        op component))
217           (:error
218            (error 'a-dot-out-failed :operation op :component component)))))
219     (multiple-value-bind (output warnings-p failure-p)
220         (compile-file tmp-constants :output-file output-file)
221       (when warnings-p
222         (case (operation-on-warnings op)
223           (:warn (warn
224                   (formatter "~@<COMPILE-FILE warned while ~
225                               performing ~A on ~A.~@:>")
226                   op component))
227           (:error (error 'compile-warned :component component :operation op))
228           (:ignore nil)))
229       (when failure-p
230         (case (operation-on-failure op)
231           (:warn (warn
232                   (formatter "~@<COMPILE-FILE failed while ~
233                               performing ~A on ~A.~@:>")
234                   op component))
235           (:error (error 'compile-failed :component component :operation op))
236           (:ignore nil)))
237       (unless output
238         (error 'compile-error :component component :operation op)))))
239