Debian Codegraph @

src/cmp/cmppass1-top.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 t1expr (form)
13 (let* ((*current-toplevel-form* nil)
14 (*cmp-env* (if *cmp-env*
15 (cmp-env-copy *cmp-env*)
16 (cmp-env-root))))
17 (push (t1expr* form) *top-level-forms*)))
18
19 (defvar *toplevel-forms-to-print*
20 '(defun defmacro defvar defparameter defclass defmethod defgeneric))
21
22 (defun t1expr* (form &aux
23 (*current-toplevel-form* (list* form *current-toplevel-form*))
24 (*current-form* form)
25 (*setjmps* 0))
26 (setq form (chk-symbol-macrolet form))
27 (unless (consp form)
28 (return-from t1expr* (t1ordinary 'NIL)))
29 (let ((fun (car form)) (args (cdr form)) fd)
30 (when (member fun *toplevel-forms-to-print*)
31 (print-current-form))
32 (cond
33 ((consp fun) (t1ordinary form))
34 ((not (symbolp fun))
35 (cmperr "~s is illegal function." fun))
36 ((eq fun 'QUOTE)
37 (t1ordinary 'NIL))
38 ((setq fd (gethash fun *t1-dispatch-table*))
39 (funcall fd args))
40 ((gethash fun *c1-dispatch-table*)
41 (t1ordinary form))
42 ((and (setq fd (cmp-compiler-macro-function fun))
43 (inline-possible fun)
44 (let ((success nil))
45 (multiple-value-setq (fd success)
46 (cmp-expand-macro fd form))
47 success))
48 (when *compile-time-too*
49 ;; Ignore compiler macros during compile time evaluation
50 ;; (they may expand in ffi:c-inline which the bytecodes
51 ;; compiler can't execute).
52 (cmp-eval form))
53 (let ((*compile-time-too* nil))
54 (push 'macroexpand *current-toplevel-form*)
55 (t1expr* fd)))
56 ((setq fd (cmp-macro-function fun))
57 (push 'macroexpand *current-toplevel-form*)
58 (t1expr* (cmp-expand-macro fd form)))
59 (t (t1ordinary form)))))
60
61 (defun t1/c1expr (form)
62 (if (not *compile-toplevel*)
63 (c1expr form)
64 (t1expr* form)))
65
66 (defun c1eval-when (args)
67 (check-args-number 'EVAL-WHEN args 1)
68 (let ((load-flag nil)
69 (compile-flag nil)
70 (execute-flag nil))
71 (dolist (situation (car args))
72 (case situation
73 ((CL:LOAD :LOAD-TOPLEVEL) (setq load-flag t))
74 ((CL:COMPILE :COMPILE-TOPLEVEL) (setq compile-flag t))
75 ((CL:EVAL :EXECUTE)
76 (if *compile-toplevel*
77 (setq compile-flag (or *compile-time-too* compile-flag))
78 (setq execute-flag t)))
79 (otherwise (cmperr "The EVAL-WHEN situation ~s is illegal."
80 situation))))
81 (cond ((not *compile-toplevel*)
82 (c1progn (and execute-flag (rest args))))
83 (load-flag
84 (let ((*compile-time-too* compile-flag))
85 (c1progn (rest args))))
86 (compile-flag
87 (cmp-eval (cons 'PROGN (rest args)))
88 (c1progn 'NIL))
89 (t
90 (c1progn 'NIL)))))
91
92 (defun t1ordinary (form)
93 (when *compile-time-too* (cmp-eval form))
94 (let ((*compile-toplevel* nil)
95 (*compile-time-too* nil))
96 (add-load-time-values (make-c1form* 'ORDINARY :args (c1expr form)))))
97
98 (defun add-load-time-values (form)
99 (let ((previous (append (and (consp *load-time-values*)
100 (nreverse *load-time-values*))
101 (nreverse *make-forms*))))
102 (when previous
103 (setf *load-time-values* nil
104 *make-forms* nil)
105 (setf form (make-c1form* 'PROGN :args (nconc previous (list form))))))
106 form)
107
108 (defun t1defmacro (args)
109 (check-args-number 'LOAD-TIME-VALUE args 2)
110 (destructuring-bind (name lambda-list &rest body)
111 args
112 (multiple-value-bind (function pprint doc-string)
113 (si:expand-defmacro name lambda-list body)
114 (declare (ignore pprint doc-string))
115 (let ((fn (cmp-eval function *cmp-env*)))
116 (cmp-env-register-global-macro name fn))
117 (t1expr* (macroexpand `(DEFMACRO ,@args))))))
118
119 (defun c1load-time-value (args)
120 (check-args-number 'LOAD-TIME-VALUE args 1 2)
121 (let ((form (first args))
122 loc)
123 (cond ((not (listp *load-time-values*))
124 ;; When using COMPILE, we set *load-time-values* to 'VALUES and
125 ;; thus signal that we do not want to compile these forms, but
126 ;; just to retain their value.
127 (return-from c1load-time-value
128 (c1constant-value (cmp-eval form))))
129 ((typep form '(or list symbol))
130 (setf loc (data-empty-loc))
131 (push (make-c1form* 'LOAD-TIME-VALUE :args loc (c1expr form))
132 *load-time-values*))
133 (t
134 (setf loc (add-object (cmp-eval form)))))
135 (make-c1form* 'LOCATION :type t :args loc)))
136
137 ;;; ----------------------------------------------------------------------------
138 ;;; Optimizer for FSET. Removes the need for a special handling of DEFUN as a
139 ;;; toplevel form and also allows optimizing calls to DEFUN or DEFMACRO which
140 ;;; are not toplevel, but which create no closures.
141 ;;;
142 ;;; The idea is as follows: when the function or macro to be defined is not a
143 ;;; closure, we can use the auxiliary C functions c_def_c_*() instead of
144 ;;; creating a closure and invoking si_fset(). However until the C2 phase of
145 ;;; the compiler we do not know whether a function is a closure, hence the need
146 ;;; for a c2fset.
147 ;;;
148 ;;; We optimize:
149 ;;;
150 ;;; (SYS:FSET NAME #'(LAMBDA ...) ...)
151 ;;;
152 ;;; where LAMBDA is expanded by C1FUNCTION to:
153 ;;;
154 ;;; (SYS:FSET NAME (FLET ((FOO ...)) #'FOO))
155 ;;;
156 (defun t1fset (args)
157 (let ((form `(si::fset ,@args)))
158 (when *compile-time-too*
159 (cmp-eval form))
160 (let ((*compile-toplevel* nil)
161 (*compile-time-too* nil))
162 (add-load-time-values (c1fset form)))))
163
164 (defun c1fset (form)
165 (destructuring-bind (fname def &optional (macro nil) (pprint nil))
166 (rest form)
167 (let* ((*use-c-global* t)
168 (fun-form (c1expr def)))
169 (when (eq (c1form-name fun-form) 'LOCALS)
170 (let* ((function-list (c1form-arg 0 fun-form))
171 (fun-object (pop function-list))
172 (form (c1form-arg 1 fun-form)))
173 (when (and
174 ;; Only 1 function
175 (null function-list)
176 ;; Not closed over anything
177 (every #'global-var-p (fun-referenced-vars fun-object))
178 ;; Referencing the function variable
179 (eq (c1form-name form) 'VARIABLE)
180 (eq (c1form-arg 0 form) (fun-var fun-object)))
181 (when (fun-no-entry fun-object)
182 (when macro
183 (cmperr "Declaration C-LOCAL used in macro ~a." fname))
184 (return-from c1fset
185 (make-c1form* 'SI:FSET :args fun-object nil nil nil nil)))
186 (when (and (typep macro 'boolean)
187 (typep pprint '(or integer null))
188 (consp fname)
189 (eq (first fname) 'quote))
190 (return-from c1fset
191 (make-c1form* 'SI:FSET :args
192 fun-object ;; Function object
193 (let ((fname (second fname)))
194 (add-object fname :permanent t
195 :duplicate t
196 :always t
197 :used-p t))
198 macro
199 pprint
200 ;; The c1form, when we do not optimize
201 (list (c1expr fname)
202 fun-form
203 (c1expr macro)
204 (c1expr pprint)))))))))
205 (t1ordinary form)))