Debian Codegraph @

src/util/gen-code.lisp

raw
1 ;;;
2 ;;; Writing src/h/cons.h and src/c/cons.d
3 ;;;
4
5 (defun process-file (filename)
6 (let* ((filename (merge-pathnames filename))
7 (name (pathname-name filename))
8 (input (make-pathname :name (concatenate 'string "bak-" name)
9 :type (pathname-type filename)
10 :directory (pathname-directory filename)))
11 (output filename))
12 (cond ((not (probe-file filename))
13 (error "Unable to find ~a" filename))
14 ((probe-file input)
15 (error "Backup already exists" input))
16 (t
17 (format t "~%;;; Renaming ~a -> ~a" filename input)
18 (rename-file filename input)))
19 (with-open-file (in input :direction :input)
20 (with-open-file (out output :direction :output :if-exists :supersede)
21 (format t "~%;;; Transforming ~a -> ~a" input output)
22 (loop with skip = nil
23 for l = (read-line in nil nil nil)
24 while l
25 do (cond (skip
26 (when (search "END-GENERATED" l)
27 (write-line l out)
28 (setf skip nil)))
29 (t
30 (write-line l out)
31 (let ((ndx (search "BEGIN-GENERATED" l)))
32 (when ndx
33 (let* ((*standard-output* out)
34 (form-text (subseq l (+ ndx 15)))
35 (form (read-from-string form-text)))
36 (eval form)
37 (setf skip t)))))))))
38 (format t "~%;;; Deleting the file ~a" input)
39 (delete-file input)))
40
41 (defun write-rec (depth list flag &optional (prefix ""))
42 (when (plusp depth)
43 (write-rec (1- depth) (cons 'a list) flag)
44 (write-rec (1- depth) (cons 'd list) flag)
45 (return-from write-rec))
46 (let* ((string (apply #'concatenate 'string (mapcar #'string-downcase list))))
47 (case flag
48 (:inline
49 (write-rec depth list :unsafe "static ECL_INLINE "))
50 (:unsafe-macro
51 (format t "~%#define C~AR(x) _ecl_c~ar(x)" (string-upcase string) string))
52 (:unsafe
53 (format t "~%~acl_object _ecl_c~ar(cl_object x)~%{" prefix string)
54 (loop for what in (reverse list)
55 for op = (if (eq what 'a) "ECL_CONS_CAR" "ECL_CONS_CDR")
56 do (format t "~% if (Null(x)) return x;~% x = ~A(x);" op))
57 (format t "~% return x;~%}~%"))
58 (:safe
59 (format t "~%cl_object ecl_c~ar(cl_object x)~%{" string)
60 (loop for what in (reverse list)
61 for op = (if (eq what 'a) "ECL_CONS_CAR" "ECL_CONS_CDR")
62 do (format t "~% if (ecl_unlikely(!ECL_LISTP(x))) FEwrong_type_nth_arg(@[car], 1, x, @[list]);")
63 do (format t "~% if (Null(x)) return x;~% x = ~A(x);" op))
64 (format t "~% return x;~%}~%"))
65 (:common-lisp
66 (format t "~%cl_object cl_c~ar(cl_object x)~%{~% return1(ecl_c~ar(x));~%}~%"
67 string string))
68 (:declare-unsafe
69 (format t "~%extern ECL_API cl_object _ecl_c~ar(cl_object);" string))
70 (:declare-safe
71 (format t "~%extern ECL_API cl_object ecl_c~ar(cl_object);" string))
72 (:declare-common-lisp
73 (format t "~%extern ECL_API cl_object cl_c~ar(cl_object);" string))
74 (:common-lisp-inline
75 (format t "~%(def-inline c~ar :always (t) t \"ecl_c~ar(#0)\")" string string)
76 (format t "~%(def-inline c~ar :unsafe (t) t \"_ecl_c~ar(#0)\")" string string)
77 )
78 )))
79
80 (defun gen-cons-h ()
81 (format t "~%#if ECL_CAN_INLINE")
82 (loop for depth from 1 below 5
83 do (write-rec depth nil :inline))
84 (format t "~%#else")
85 (loop for depth from 1 below 5
86 do (write-rec depth nil :declare-unsafe))
87 (format t "~%#endif /* !ECL_CAN_INLINE */~%")
88 (loop for depth from 1 below 5
89 do (write-rec depth nil :declare-safe))
90 (terpri)
91 (gen-cons-legacy-h)
92 (loop for depth from 1 below 5
93 do (write-rec depth nil :declare-common-lisp))
94 (terpri))
95
96 (defun gen-cons-d ()
97 (format t "~%#if !ECL_CAN_INLINE")
98 (loop for depth from 1 below 5
99 do (write-rec depth nil :unsafe))
100 (format t "~%#endif /* !ECL_CAN_INLINE */~%")
101 (loop for depth from 1 below 5
102 do (write-rec depth nil :safe))
103 (terpri)
104 (loop for depth from 1 below 5
105 do (write-rec depth nil :common-lisp))
106 (terpri))
107
108 (defun gen-cons-legacy-h ()
109 (loop for depth from 1 below 5
110 do (write-rec depth nil :unsafe-macro))
111 (terpri))
112
113 (defun gen-cons-sysfun ()
114 (loop for depth from 1 below 5
115 do (write-rec depth nil :common-lisp-inline))
116 (terpri))
117
118 (process-file "src/c/cons.d")
119 (process-file "src/h/cons.h")
120 ;(process-file "src/h/legacy.h")
121 (process-file "src/cmp/sysfun.lsp")
122 (terpri)
123 #+ecl
124 (ext:quit)