1 ;;;; extensions which are needed in order to (cross-)compile target-only code
3 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
12 (in-package "SB!IMPL")
14 ;;; Lots of code wants to get to the KEYWORD package or the
15 ;;; COMMON-LISP package without a lot of fuss, so we cache them in
16 ;;; variables. TO DO: How much does this actually buy us? It sounds
17 ;;; sensible, but I don't know for sure that it saves space or time..
20 ;;; (The initialization forms here only matter on the cross-compilation
21 ;;; host; In the target SBCL, these variables are set in cold init.)
22 (declaim (type package *cl-package* *keyword-package*))
23 (defvar *cl-package* (find-package "COMMON-LISP"))
24 (defvar *keyword-package* (find-package "KEYWORD"))
26 ;;; a helper function for various macros which expect clauses of a
27 ;;; given length, etc.
29 ;;; FIXME: This implementation will hang on circular list structure.
30 ;;; Since this is an error-checking utility, i.e. its job is to deal
31 ;;; with screwed-up input, it'd be good style to fix it so that it can
32 ;;; deal with circular list structure.
33 (eval-when (:compile-toplevel :load-toplevel :execute)
34 ;; Return true if X is a proper list whose length is between MIN and
36 (defun proper-list-of-length-p (x min &optional (max min))
43 (proper-list-of-length-p (cdr x)
50 ;;;; the COLLECT macro
52 ;;; helper functions for COLLECT, which become the expanders of the
53 ;;; MACROLET definitions created by COLLECT
55 ;;; COLLECT-NORMAL-EXPANDER handles normal collection macros.
57 ;;; COLLECT-LIST-EXPANDER handles the list collection case. N-TAIL
58 ;;; is the pointer to the current tail of the list, or NIL if the list
60 (defun collect-normal-expander (n-value fun forms)
62 ,@(mapcar #'(lambda (form) `(setq ,n-value (,fun ,form ,n-value))) forms)
64 (defun collect-list-expander (n-value n-tail forms)
65 (let ((n-res (gensym)))
67 ,@(mapcar #'(lambda (form)
68 `(let ((,n-res (cons ,form nil)))
70 (setf (cdr ,n-tail) ,n-res)
71 (setq ,n-tail ,n-res))
73 (setq ,n-tail ,n-res ,n-value ,n-res)))))
77 ;;; the ultimate collection macro...
78 (defmacro collect (collections &body body)
80 "Collect ({(Name [Initial-Value] [Function])}*) {Form}*
81 Collect some values somehow. Each of the collections specifies a bunch of
82 things which collected during the evaluation of the body of the form. The
83 name of the collection is used to define a local macro, a la MACROLET.
84 Within the body, this macro will evaluate each of its arguments and collect
85 the result, returning the current value after the collection is done. The
86 body is evaluated as a PROGN; to get the final values when you are done, just
87 call the collection macro with no arguments.
89 INITIAL-VALUE is the value that the collection starts out with, which
90 defaults to NIL. FUNCTION is the function which does the collection. It is
91 a function which will accept two arguments: the value to be collected and the
92 current collection. The result of the function is made the new value for the
93 collection. As a totally magical special-case, FUNCTION may be COLLECT,
94 which tells us to build a list in forward order; this is the default. If an
95 INITIAL-VALUE is supplied for Collect, the stuff will be RPLACD'd onto the
96 end. Note that FUNCTION may be anything that can appear in the functional
97 position, including macros and lambdas."
101 (dolist (spec collections)
102 (unless (proper-list-of-length-p spec 1 3)
103 (error "Malformed collection specifier: ~S." spec))
104 (let* ((name (first spec))
105 (default (second spec))
106 (kind (or (third spec) 'collect))
107 (n-value (gensym (concatenate 'string
110 (push `(,n-value ,default) binds)
111 (if (eq kind 'collect)
112 (let ((n-tail (gensym (concatenate 'string
116 (push `(,n-tail (last ,n-value)) binds)
118 (push `(,name (&rest args)
119 (collect-list-expander ',n-value ',n-tail args))
121 (push `(,name (&rest args)
122 (collect-normal-expander ',n-value ',kind args))
124 `(macrolet ,macros (let* ,(nreverse binds) ,@body))))
126 ;;; This function can be used as the default value for keyword
127 ;;; arguments that must be always be supplied. Since it is known by
128 ;;; the compiler to never return, it will avoid any compile-time type
129 ;;; warnings that would result from a default value inconsistent with
130 ;;; the declared type. When this function is called, it signals an
131 ;;; error indicating that a required &KEY argument was not supplied.
132 ;;; This function is also useful for DEFSTRUCT slot defaults
133 ;;; corresponding to required arguments.
134 (declaim (ftype (function () nil) required-argument))
135 (defun required-argument ()
137 (/show0 "entering REQUIRED-ARGUMENT")
138 (error "A required &KEY argument was not supplied."))
140 ;;; "the ultimate iteration macro"
142 ;;; note for Schemers: This seems to be identical to Scheme's "named LET".
143 (defmacro named-let (name binds &body body)
146 (unless (proper-list-of-length-p x 2)
147 (error "Malformed ITERATE variable spec: ~S." x)))
148 `(labels ((,name ,(mapcar #'first binds) ,@body))
149 (,name ,@(mapcar #'second binds))))
151 ;;; ONCE-ONLY is a utility useful in writing source transforms and
152 ;;; macros. It provides a concise way to wrap a LET around some code
153 ;;; to ensure that some forms are only evaluated once.
155 ;;; Create a LET* which evaluates each value expression, binding a
156 ;;; temporary variable to the result, and wrapping the LET* around the
157 ;;; result of the evaluation of BODY. Within the body, each VAR is
158 ;;; bound to the corresponding temporary variable.
159 (defmacro once-only (specs &body body)
160 (named-let frob ((specs specs)
164 (let ((spec (first specs)))
165 ;; FIXME: should just be DESTRUCTURING-BIND of SPEC
166 (unless (proper-list-of-length-p spec 2)
167 (error "malformed ONCE-ONLY binding spec: ~S" spec))
168 (let* ((name (first spec))
169 (exp-temp (gensym (symbol-name name))))
170 `(let ((,exp-temp ,(second spec))
171 (,name (gensym "ONCE-ONLY-")))
172 `(let ((,,name ,,exp-temp))
173 ,,(frob (rest specs) body))))))))
175 ;;;; some old-fashioned functions. (They're not just for old-fashioned
176 ;;;; code, they're also used as optimized forms of the corresponding
177 ;;;; general functions when the compiler can prove that they're
180 ;;; like (MEMBER ITEM LIST :TEST #'EQ)
181 (defun memq (item list)
183 "Returns tail of LIST beginning with first element EQ to ITEM."
184 ;; KLUDGE: These could be and probably should be defined as
185 ;; (MEMBER ITEM LIST :TEST #'EQ)),
186 ;; but when I try to cross-compile that, I get an error from
187 ;; LTN-ANALYZE-KNOWN-CALL, "Recursive known function definition". The
188 ;; comments for that error say it "is probably a botched interpreter stub".
189 ;; Rather than try to figure that out, I just rewrote this function from
190 ;; scratch. -- WHN 19990512
191 (do ((i list (cdr i)))
193 (when (eq (car i) item)
196 ;;; like (ASSOC ITEM ALIST :TEST #'EQ)
197 (defun assq (item alist)
199 "Return the first pair of ALIST where ITEM is EQ to the key of the pair."
200 ;; KLUDGE: CMU CL defined this with
201 ;; (DECLARE (INLINE ASSOC))
202 ;; (ASSOC ITEM ALIST :TEST #'EQ))
203 ;; which is pretty, but which would have required adding awkward
204 ;; build order constraints on SBCL (or figuring out some way to make
205 ;; inline definitions installable at build-the-cross-compiler time,
206 ;; which was too ambitious for now). Rather than mess with that, we
207 ;; just define ASSQ explicitly in terms of more primitive
210 (when (eq (car pair) item)
213 (defun delq (item list)
215 "Delete all LIST entries EQ to ITEM (destructively modifying LIST), and
216 return the modified LIST."
218 (do ((x list (cdr x))
221 (cond ((eq item (car x))
224 (rplacd splice (cdr x))))
225 (t (setq splice x)))))) ; Move splice along to include element.
228 ;; (defmacro posq (item list) `(position ,item ,list :test #'eq))
229 (defun posq (item list)
231 "Returns the position of the first element EQ to ITEM."
232 (do ((i list (cdr i))
235 (when (eq (car i) item)
238 ;; (defmacro neq (x y) `(not (eq ,x ,y)))
239 (defun neq (x y) (not (eq x y)))