Debian Codegraph @

src/cmp/cmpenv-fun.lsp

raw
1 ;;;; -*- Mode: Lisp; Syntax: Common-Lisp; indent-tabs-mode: nil; Package: C -*-
2 ;;;; vim: set filetype=lisp tabstop=8 shiftwidth=2 expandtab:
3
4 ;;;;
5 ;;;; Copyright (c) 2009, Juan Jose Garcia-Ripoll.
6 ;;;;
7 ;;;; See file 'LICENSE' for the copyright details.
8 ;;;;
9
10 ;;;;
11 ;;;; CMPENV-FUN -- Declarations concerning function types and inlining
12 ;;;;
13
14 (in-package "COMPILER")
15
16 (defun function-arg-types (arg-types &aux (types nil))
17 (do ((al arg-types (cdr al)))
18 ((or (endp al)
19 (member (car al) '(&optional &rest &key)))
20 (nreverse types))
21 (declare (object al))
22 (push (car al) types)))
23
24 ;;; The valid return type declaration is:
25 ;;; (( VALUES {type}* )) or ( {type}* ).
26
27 (defun proclaim-function (fname decl)
28 (if (si:valid-function-name-p fname)
29 (let* ((arg-types '*)
30 (return-types '*)
31 (l decl))
32 (cond ((null l))
33 ((consp l)
34 (setf arg-types (pop l)))
35 (t (warn "The function proclamation ~s ~s is not valid."
36 fname decl)))
37 (cond ((null l))
38 ((or (atom l) (rest l))
39 (warn "The function proclamation ~s ~s is not valid."
40 fname decl))
41 (t
42 (setf return-types (first l))))
43 (when (eq arg-types '())
44 (setf arg-types '(&optional)))
45 (if (eq arg-types '*)
46 (si:rem-sysprop fname 'PROCLAIMED-ARG-TYPES)
47 (si:put-sysprop fname 'PROCLAIMED-ARG-TYPES arg-types))
48 (if (member return-types '(* (VALUES &rest t))
49 :test #'equalp)
50 (si:rem-sysprop fname 'PROCLAIMED-RETURN-TYPE)
51 (si:put-sysprop fname 'PROCLAIMED-RETURN-TYPE return-types)))
52 (warn "The function proclamation ~s ~s is not valid." fname decl)))
53
54 (defun add-function-declaration (fname ftype &optional (env *cmp-env*))
55 (if (si::valid-function-name-p fname)
56 (let ((fun (cmp-env-search-function fname)))
57 (if (functionp fun)
58 (warn "Found function declaration for local macro ~A" fname)
59 (cmp-env-register-ftype fname ftype env)))
60 (warn "In (DECLARE (FTYPE ...)): ~s is not a valid function name" fname))
61 env)
62
63 (defun get-arg-types (fname &optional (env *cmp-env*) (may-be-global t))
64 (ext:if-let ((x (cmp-env-search-ftype fname env)))
65 (let ((arg-types (first x)))
66 (unless (eq arg-types '*)
67 (values arg-types t)))
68 (when may-be-global
69 (let ((fun (cmp-env-search-function fname env)))
70 (when (or (null fun) (and (fun-p fun) (fun-global fun)))
71 (si:get-sysprop fname 'PROCLAIMED-ARG-TYPES))))))
72
73 (defun get-return-type (fname &optional (env *cmp-env*))
74 (ext:if-let ((x (cmp-env-search-ftype fname env)))
75 (let ((return-types (second x)))
76 (unless (eq return-types '*)
77 (values return-types t)))
78 (let ((fun (cmp-env-search-function fname env)))
79 (when (or (null fun) (and (fun-p fun) (fun-global fun)))
80 (si:get-sysprop fname 'PROCLAIMED-RETURN-TYPE)))))
81
82 (defun get-local-arg-types (fun &optional (env *cmp-env*))
83 (ext:if-let ((x (cmp-env-search-ftype (fun-name fun) env)))
84 (values (first x) t)
85 (values nil nil)))
86
87 (defun get-local-return-type (fun &optional (env *cmp-env*))
88 (ext:if-let ((x (cmp-env-search-ftype (fun-name fun) env)))
89 (values (second x) t)
90 (values nil nil)))
91
92 (defun get-proclaimed-narg (fun &optional (env *cmp-env*))
93 (multiple-value-bind (arg-list found)
94 (get-arg-types fun env)
95 (if found
96 (loop for type in arg-list
97 with minarg = 0
98 and maxarg = 0
99 and in-optionals = nil
100 do (cond ((member type '(* &rest &key &allow-other-keys) :test #'eq)
101 (return (values minarg call-arguments-limit found)))
102 ((eq type '&optional)
103 (setf in-optionals t maxarg minarg))
104 (in-optionals
105 (incf maxarg))
106 (t
107 (incf minarg)
108 (incf maxarg)))
109 finally (return (values minarg maxarg found)))
110 (values 0 call-arguments-limit found))))
111
112 ;;; Proclamation and declaration handling.
113
114 (defun declare-inline (fname &optional (env *cmp-env*) (definition t))
115 (unless (si::valid-function-name-p fname)
116 (cmperr "Not a valid argument to INLINE declaration~%~4I~A"
117 fname))
118 (cmp-env-extend-declaration 'INLINE (list (cons fname definition)) env))
119
120 (defun declare-notinline (fname &optional (env *cmp-env*))
121 (declare-inline fname env nil))
122
123 (defun proclaim-inline (fname-list)
124 (dolist (fun fname-list)
125 (unless (si::valid-function-name-p fun)
126 (error "Not a valid function name ~s in INLINE proclamation" fun))
127 (unless (si:get-sysprop fun 'INLINE)
128 (si:put-sysprop fun 'INLINE t)
129 (si:rem-sysprop fun 'NOTINLINE))))
130
131 (defun proclaim-notinline (fname-list)
132 (dolist (fun fname-list)
133 (unless (si::valid-function-name-p fun)
134 (error "Not a valid function name ~s in NOTINLINE proclamation" fun))
135 (si:rem-sysprop fun 'INLINE)
136 (si:put-sysprop fun 'NOTINLINE t)))
137
138 (defun declared-inline-p (fname &optional (env *cmp-env*))
139 (let* ((x (cmp-env-search-declaration 'inline env))
140 (flag (assoc fname x :test #'same-fname-p)))
141 (if flag
142 (cdr flag)
143 (si:get-sysprop fname 'INLINE))))
144
145 (defun declared-notinline-p (fname &optional (env *cmp-env*))
146 (let* ((x (cmp-env-search-declaration 'inline env))
147 (flag (assoc fname x :test #'same-fname-p)))
148 (if flag
149 (null (cdr flag))
150 (si:get-sysprop fname 'NOTINLINE))))
151
152 (defun inline-possible (fname &optional (env *cmp-env*))
153 (not (declared-notinline-p fname env)))
154
155 ;;; Install inline expansion of function.
156 (defun maybe-install-inline-function (fname form)
157 (when (declared-inline-p fname *cmp-env-root*)
158 ;; The function was already PROCLAIMED inline and might be
159 ;; redefined in the file we are currently compiling. Declare it as
160 ;; inline in the compiler environment and remove the symbol
161 ;; property so that if we can't inline the new definition (e.g.
162 ;; because it is a closure) we don't accidentally inline an old
163 ;; definition from the symbol property.
164 (declare-inline fname *cmp-env-root*)
165 (si:rem-sysprop fname 'inline)
166 ;; If the function is PROCLAIMED or DECLAIMED inline, then we
167 ;; install the definition as a symbol property during loading of
168 ;; the compiled file. If the function was only DECLARED inline
169 ;; locally we don't keep the definition.
170 `(eval-when (:load-toplevel :execute)
171 (si:put-sysprop ',fname 'inline ',form))))
172
173 (defun set-closure-env (definition lexenv &optional (env *cmp-env*))
174 "Set up an environment for compilation of closures: Register closed
175 over macros in the compiler environment and enclose the definition of
176 the closure in let/flet forms for variables/functions it closes over."
177 (flet ((handle-record (record)
178 (cond
179 ((not (listp record))
180 (multiple-value-bind (record-def record-lexenv)
181 (function-lambda-expression record)
182 (let* ((self-ref (position record record-lexenv))
183 (flet-env (remove record record-lexenv)))
184 (case (car record-def)
185 (CL:LAMBDA
186 (setf record-def (cdr record-def)))
187 (EXT:LAMBDA-BLOCK
188 (setf record-def (cddr record-def)))
189 (otherwise
190 (error "~&;;; Error: Not a valid lambda expression: ~s."
191 record-def)))
192 ;; Allow for closures that close over other closures.
193 ;; (first record-def) is the lambda list, (rest record-def)
194 ;; the definition of the local function in the record.
195 (setf (rest record-def)
196 (list (set-closure-env (if (= (length record-def) 2)
197 (second record-def)
198 `(progn ,@(rest record-def)))
199 flet-env env)))
200 (setf definition
201 (if self-ref
202 `(labels ((,(ext:compiled-function-name record)
203 ,@record-def))
204 ,definition)
205 `(flet ((,(ext:compiled-function-name record)
206 ,@record-def))
207 ,definition))))))
208 ((and (listp record) (symbolp (car record)))
209 (cond ((eq (car record) 'si:macro)
210 (cmp-env-register-macro (cddr record) (cadr record) env))
211 ((eq (car record) 'si:symbol-macro)
212 (cmp-env-register-symbol-macro-function (cddr record)
213 (cadr record)
214 env))
215 (t
216 (setf definition
217 `(let ((,(car record) ',(cdr record)))
218 ,definition)))
219 ))
220 ;; ((and (integerp (cdr record)) (= (cdr record) 0))
221 ;; Tags: We have lost the information, which tag
222 ;; corresponds to the lex-env record. If we are
223 ;; compiling a closure over a tag, we will get an
224 ;; error later on.
225 ;; )
226 ;; (t
227 ;; Blocks: Not yet implemented
228 )))
229 (map nil #'handle-record lexenv)
230 definition))