Debian Codegraph @

src/cmp/cmppass1-eval.lsp

raw
1 ;;;;
2 ;;;; Copyright (c) 1984, Taiichi Yuasa and Masami Hagiya
3 ;;;; Copyright (c) 1990, Giuseppe Attardi
4 ;;;; Copyright (c) 2010, Juan Jose Garcia-Ripoll
5 ;;;; Copyright (c) 2021, Daniel Kochmański
6 ;;;;
7 ;;;; See file 'LICENSE' for the copyright details.
8 ;;;;
9
10 (in-package #:compiler)
11
12 (defun c1expr-inner (form)
13 (declare (si::c-local))
14 (cond ((symbolp form)
15 (setq form (chk-symbol-macrolet form))
16 (cond ((not (symbolp form))
17 form)
18 ((eq form nil) (c1nil))
19 ((eq form t) (c1t))
20 ((keywordp form)
21 (make-c1form* 'LOCATION :type (object-type form)
22 :args (add-symbol form)))
23 ((constantp form *cmp-env*)
24 (c1variable form (c1constant-symbol-value form (symbol-value form))))
25 (t
26 (c1variable form nil))))
27 ((consp form)
28 (cmpck (not (si:proper-list-p form))
29 "Improper list found in lisp form~%~A" form)
30 (let ((fun (car form)))
31 (cond ((let ((fd (gethash fun *c1-dispatch-table*)))
32 (and fd (setf fun fd)))
33 (funcall fun (rest form)))
34 ((symbolp fun)
35 (c1call fun (cdr form) t))
36 ((and (consp fun) (eq (car fun) 'LAMBDA))
37 (c1funcall form))
38 (t (cmperr "~s is not a legal function name." fun)))))
39 (t (c1constant-value form))))
40
41 (defun c1expr (form)
42 (let ((*current-form* form))
43 (loop
44 (setf form (c1expr-inner form))
45 (when (c1form-p form)
46 (return form)))))
47
48 (defvar *vv-nil*
49 (make-vv :value CL:NIL))
50
51 (defvar *vv-t*
52 (make-vv :value CL:T))
53
54 (defun c1nil ()
55 (let ((*current-form* nil))
56 (make-c1form* 'LOCATION :type 'null :args *vv-nil*)))
57 (defun c1t ()
58 (let ((*current-form* t))
59 (make-c1form* 'LOCATION :type '(eql t) :args *vv-t*)))
60
61 (defun c1with-backend (forms)
62 (c1progn (loop for tag = (pop forms)
63 for form = (pop forms)
64 while tag
65 when (eq tag :c/c++)
66 collect form)))
67
68 (defun c1progn (forms)
69 (cond ((endp forms) (t1/c1expr 'NIL))
70 ((endp (cdr forms)) (t1/c1expr (car forms)))
71 (t (let* ((fl (mapcar #'t1/c1expr forms))
72 (output-form (first (last fl)))
73 (output-type (and output-form (c1form-type output-form))))
74 (make-c1form* 'PROGN :type output-type :args fl)))))
75
76 (defun c1args* (forms)
77 (mapcar #'c1expr forms))
78
79 (defun c1decl-body (decls body)
80 (if (null decls)
81 (c1progn body)
82 (let* ((*cmp-env* (reduce #'add-one-declaration decls
83 :initial-value (cmp-env-copy *cmp-env*))))
84 (c1progn body))))
85
86 (defun c1locally (args)
87 (multiple-value-bind (body ss ts is other-decl)
88 (c1body args t)
89 (if (or ss ts is other-decl)
90 (let ((*cmp-env* (cmp-env-copy)))
91 (mapc #'declare-special ss)
92 (check-vdecl nil ts is)
93 (c1decl-body other-decl body))
94 (c1progn body))))
95
96 (defun c1macrolet (args)
97 (check-args-number 'MACROLET args 1)
98 (let ((*cmp-env* (si:cmp-env-register-macrolet (first args) (cmp-env-copy))))
99 (c1locally (cdr args))))
100
101 (defun c1symbol-macrolet (args)
102 (check-args-number 'SYMBOL-MACROLET args 1)
103 (let ((*cmp-env* (cmp-env-copy)))
104 (dolist (def (car args))
105 (let ((name (first def)))
106 (cmpck (or (endp def) (not (symbolp name)) (endp (cdr def)))
107 "The symbol-macro definition ~s is illegal." def)
108 (cmp-env-register-symbol-macro name (second def))))
109 (c1locally (cdr args))))
110
111 ;;;
112 ;;; Check if the symbol has a symbol macro
113 ;;;
114 (defun chk-symbol-macrolet (form)
115 (loop
116 (when (not (symbolp form))
117 (return form))
118 (let ((new-form (macroexpand-1 form *cmp-env*)))
119 (when (eq new-form form)
120 (return form))
121 (setf form new-form))))
122
123 (defun c1constant-value (val)
124 (cond
125 ((eq val nil) (c1nil))
126 ((eq val t) (c1t))
127 ((make-c1form* 'LOCATION :type `(eql ,val)
128 :args (add-object val)))))
129
130 ;;; To inline a constant it must be possible to externalize its value or copies
131 ;;; of the value must be EQL to each other.
132 (defun c1constant-symbol-value (name val)
133 (declare (ignore name))
134 (unless (si:need-to-make-load-form-p val)
135 (add-object val)))
136
137 (defun c1if (args)
138 (check-args-number 'IF args 2 3)
139 (let ((test (c1expr (car args))))
140 ;; Resolve IF expressions with constant arguments
141 (multiple-value-bind (constant-p value)
142 (c1form-constant-p test)
143 (when constant-p
144 (return-from c1if
145 (if value (second args) (third args)))))
146 ;; Otherwise, normal IF form
147 (let* ((true-branch (c1expr (second args)))
148 (false-branch (c1expr (third args))))
149 (make-c1form* 'IF
150 :type (values-type-or (c1form-type true-branch)
151 (c1form-type false-branch))
152 :args test true-branch false-branch))))
153
154 (defun c1not (args)
155 (check-args-number 'NOT args 1 1)
156 (let* ((value (c1expr (first args))))
157 ;; When the argument is constant, we can just return
158 ;; a constant as well.
159 (multiple-value-bind (constant-p value)
160 (c1form-constant-p value)
161 (when constant-p
162 (return-from c1not (not value))))
163 (make-c1form* 'FMLA-NOT
164 :type '(member t nil)
165 :args value)))
166
167 (defun c1and (args)
168 ;; (AND) => T
169 (if (null args)
170 (c1t)
171 (let* ((values (c1args* args))
172 (last (first (last values)))
173 (butlast (nbutlast values)))
174 ;; (AND x) => x
175 (if butlast
176 (make-c1form* 'FMLA-AND
177 :type (type-or 'null (c1form-primary-type last))
178 :args butlast last)
179 last))))
180
181 (defun c1or (args)
182 ;; (OR) => NIL
183 (if (null args)
184 (c1nil)
185 (let* ((values (c1args* args))
186 (last (first (last values)))
187 (butlast (butlast values)))
188 ;; (OR x) => x
189 (if butlast
190 (make-c1form* 'FMLA-OR
191 :type (reduce #'type-or butlast
192 :key #'c1form-primary-type
193 :initial-value (c1form-primary-type last))
194 :args butlast last)
195 last))))
196
197 (defun c1multiple-value-prog1 (args)
198 (check-args-number 'MULTIPLE-VALUE-PROG1 args 1)
199 (destructuring-bind (form . body) args
200 (make-c1form* 'MV-PROG1 :args (c1expr form) (c1args* body))))
201
202 ;;; Beppe:
203 ;;; this is the WRONG way to handle 1 value problem.
204 ;;; should be done in c2values, so that (values (truncate a b)) can
205 ;;; be used to restrict to one value, so we would not have to warn
206 ;;; if this occurred in a proclaimed fun.
207
208 (defun c1values (args)
209 (make-c1form* 'CL:VALUES :args (c1args* args)))