/[cmucl]/src/compiler/macros.lisp
ViewVC logotype

Contents of /src/compiler/macros.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.42 - (hide annotations)
Thu Feb 25 13:03:09 1999 UTC (15 years, 1 month ago) by pw
Branch: MAIN
Changes since 1.41: +1 -9 lines
This set of changes removes nearly all references to the dylan language.
It is a benign revision as no structures are redefined. At some later
point the definitions in compiler/objdef could be removed, perhaps
in sync with some other type change.
1 wlott 1.1 ;;; -*- Package: C; Log: C.Log -*-
2     ;;;
3     ;;; **********************************************************************
4 ram 1.23 ;;; This code was written as part of the CMU Common Lisp project at
5     ;;; Carnegie Mellon University, and has been placed in the public domain.
6     ;;;
7     (ext:file-comment
8 pw 1.42 "$Header: /tiger/var/lib/cvsroots/cmucl/src/compiler/macros.lisp,v 1.42 1999/02/25 13:03:09 pw Exp $")
9 ram 1.23 ;;;
10 wlott 1.1 ;;; **********************************************************************
11     ;;;
12     ;;; Random types and macros used in writing the compiler.
13     ;;;
14     ;;; Written by Rob MacLachlan
15     ;;;
16 ram 1.32 (in-package "C")
17 wlott 1.1
18     (export '(lisp::with-compilation-unit) "LISP")
19    
20 wlott 1.12 (export '(policy symbolicate def-ir1-translator def-source-transform
21     def-primitive-translator deftransform defknown defoptimizer
22     derive-type optimizer ltn-annotate ir2-convert attributes
23     def-boolean-attribute attributes-union attributes-intersection
24     attributes=))
25    
26 wlott 1.1 (proclaim '(special *wild-type* *universal-type* *compiler-error-context*))
27    
28     ;;;; Deftypes:
29    
30     ;;;
31     ;;; Inlinep is used to determine how a function is called. The values have
32     ;;; these meanings:
33     ;;; Nil No declaration seen: do whatever you feel like, but don't dump
34     ;;; an inline expansion.
35     ;;;
36     ;;; :Notinline Notinline declaration seen: always do full function call.
37     ;;;
38     ;;; :Inline Inline declaration seen: save expansion, expanding to it if
39     ;;; policy favors.
40     ;;;
41     ;;; :Maybe-Inline
42     ;;; Retain expansion, but only use it opportunistically.
43     ;;;
44     (deftype inlinep () '(member :inline :maybe-inline :notinline nil))
45    
46    
47     ;;;; The Policy macro:
48    
49 ram 1.7 (proclaim '(special *lexical-environment*))
50 wlott 1.1
51 ram 1.22 (eval-when (compile load eval)
52 wlott 1.1 (defconstant policy-parameter-slots
53     '((speed . cookie-speed) (space . cookie-space) (safety . cookie-safety)
54     (cspeed . cookie-cspeed) (brevity . cookie-brevity)
55     (debug . cookie-debug)))
56    
57     ;;; Find-Used-Parameters -- Internal
58     ;;;
59     ;;; Find all the policy parameters which are actually mentioned in Stuff,
60     ;;; returning the names in a list. We assume everything is evaluated.
61     ;;;
62     (defun find-used-parameters (stuff)
63     (if (atom stuff)
64     (if (assoc stuff policy-parameter-slots) (list stuff) ())
65     (collect ((res () nunion))
66     (dolist (arg (cdr stuff) (res))
67     (res (find-used-parameters arg))))))
68    
69     ); Eval-When (Compile Load Eval)
70    
71     ;;; Policy -- Public
72     ;;;
73     ;;; This macro provides some syntactic sugar for querying the settings of
74     ;;; the compiler policy parameters.
75     ;;;
76     (defmacro policy (node &rest conditions)
77     "Policy Node Condition*
78     Test whether some conditions apply to the current compiler policy for Node.
79     Each condition is a predicate form which accesses the policy values by
80     referring to them as the variables SPEED, SPACE, SAFETY, CSPEED, BREVITY and
81     DEBUG. The results of all the conditions are combined with AND and returned
82     as the result.
83    
84     Node is a form which is evaluated to obtain the node which the policy is for.
85     If Node is NIL, then we use the current policy as defined by *default-cookie*
86     and *current-cookie*. This option is only well defined during IR1
87     conversion."
88     (let* ((form `(and ,@conditions))
89 ram 1.7 (n-cookie (gensym))
90 wlott 1.1 (binds (mapcar
91     #'(lambda (name)
92     (let ((slot (cdr (assoc name policy-parameter-slots))))
93 ram 1.7 `(,name (,slot ,n-cookie))))
94 wlott 1.1 (find-used-parameters form))))
95 ram 1.7 `(let* ((,n-cookie (lexenv-cookie
96     ,(if node
97     `(node-lexenv ,node)
98     '*lexical-environment*)))
99     ,@binds)
100     ,form)))
101 wlott 1.1
102    
103     ;;;; Source-hacking defining forms:
104    
105 ram 1.22 (eval-when (compile load eval)
106 wlott 1.1
107     ;;; Symbolicate -- Interface
108     ;;;
109     ;;; Concatenate together the names of some strings and symbols, producing
110     ;;; a symbol in the current package.
111     ;;;
112     (proclaim '(function symbolicate (&rest (or string symbol)) symbol))
113     (defun symbolicate (&rest things)
114     (values (intern (reduce #'(lambda (x y)
115     (concatenate 'string (string x) (string y)))
116     things))))
117    
118     ); Eval-When (Compile Load Eval)
119    
120 ram 1.10 ;;; SPECIAL-FORM-FUNCTION -- Internal
121     ;;;
122     ;;; This function is stored in the SYMBOL-FUNCTION of special form names so
123     ;;; that they are FBOUND.
124     ;;;
125     (defun special-form-function (&rest stuff)
126     (declare (ignore stuff))
127     (error "Can't funcall the SYMBOL-FUNCTION of special forms."))
128    
129 wlott 1.19 ;;; CONVERT-CONDITION-INTO-COMPILER-ERROR -- Internal
130 wlott 1.16 ;;;
131 wlott 1.19 ;;; Passed to parse-defmacro when we want compiler errors instead of real
132     ;;; errors.
133     ;;;
134     (proclaim '(inline convert-condition-into-compiler-error))
135     (defun convert-condition-into-compiler-error (datum &rest stuff)
136     (if (stringp datum)
137     (apply #'compiler-error datum stuff)
138     (compiler-error "~A"
139     (if (symbolp datum)
140     (apply #'make-condition datum stuff)
141     datum))))
142 wlott 1.16
143 wlott 1.1 ;;; Def-IR1-Translator -- Interface
144     ;;;
145     ;;; Parse defmacro style lambda-list, setting things up so that a compiler
146     ;;; error happens if the syntax is invalid.
147     ;;;
148     (defmacro def-ir1-translator (name (lambda-list start-var cont-var
149     &key (kind :special-form))
150     &body body)
151     "Def-IR1-Translator Name (Lambda-List Start-Var Cont-Var {Key Value}*)
152     [Doc-String] Form*
153     Define a function that converts a Special-Form or other magical thing into
154     IR1. Lambda-List is a defmacro style lambda list. Start-Var and Cont-Var
155     are bound to the start and result continuations for the resulting IR1.
156     This keyword is defined:
157     Kind
158     The function kind to associate with Name (default :special-form)."
159     (let ((fn-name (symbolicate "IR1-CONVERT-" name))
160     (n-form (gensym))
161     (n-env (gensym)))
162 wlott 1.15 (multiple-value-bind
163     (body decls doc)
164 wlott 1.17 (lisp::parse-defmacro lambda-list n-form body name "special form"
165 wlott 1.15 :doc-string-allowed t
166 wlott 1.16 :environment n-env
167 wlott 1.19 :error-fun 'convert-condition-into-compiler-error)
168 wlott 1.1 `(progn
169     (proclaim '(function ,fn-name (continuation continuation t) void))
170     (defun ,fn-name (,start-var ,cont-var ,n-form)
171 ram 1.7 (let ((,n-env *lexical-environment*))
172 wlott 1.1 ,@decls
173 wlott 1.16 ,body))
174 wlott 1.1 ,@(when doc
175     `((setf (documentation ',name 'function) ,doc)))
176     (setf (info function ir1-convert ',name) #',fn-name)
177 ram 1.10 (setf (info function kind ',name) ,kind)
178     #+new-compiler
179     ,@(when (eq kind :special-form)
180     `((setf (symbol-function ',name) #'special-form-function)))))))
181 wlott 1.1
182    
183     ;;; Def-Source-Transform -- Interface
184     ;;;
185     ;;; Similar to Def-IR1-Translator, except that we pass if the syntax is
186     ;;; invalid.
187     ;;;
188     (defmacro def-source-transform (name lambda-list &body body)
189     "Def-Source-Transform Name Lambda-List Form*
190     Define a macro-like source-to-source transformation for the function Name.
191     A source transform may \"pass\" by returning a non-nil second value. If the
192     transform passes, then the form is converted as a normal function call. If
193     the supplied arguments are not compatible with the specified lambda-list,
194     then the transform automatically passes.
195    
196     Source-Transforms may only be defined for functions. Source transformation
197     is not attempted if the function is declared Notinline. Source transforms
198     should not examine their arguments. If it matters how the function is used,
199     then Deftransform should be used to define an IR1 transformation.
200    
201     If the desirability of the transformation depends on the current Optimize
202     parameters, then the Policy macro should be used to determine when to pass."
203 wlott 1.33 (let ((fn-name
204     (if (listp name)
205     (collect ((pieces))
206     (dolist (piece name)
207     (pieces "-")
208     (pieces piece))
209     (apply #'symbolicate "SOURCE-TRANSFORM" (pieces)))
210     (symbolicate "SOURCE-TRANSFORM-" name)))
211 wlott 1.1 (n-form (gensym))
212     (n-env (gensym)))
213 wlott 1.15 (multiple-value-bind
214     (body decls)
215 wlott 1.17 (lisp::parse-defmacro lambda-list n-form body name "form"
216 wlott 1.16 :environment n-env
217     :error-fun `(lambda (&rest stuff)
218     (declare (ignore stuff))
219     (return-from ,fn-name
220     (values nil t))))
221 wlott 1.1 `(progn
222     (defun ,fn-name (,n-form)
223 ram 1.7 (let ((,n-env *lexical-environment*))
224 wlott 1.1 ,@decls
225 wlott 1.16 ,body))
226 wlott 1.1 (setf (info function source-transform ',name) #',fn-name)))))
227    
228    
229     (defmacro def-primitive-translator (name lambda-list &body body)
230     "Def-Primitive-Translator Name Lambda-List Form*
231     Define a function that converts a use of (%PRIMITIVE Name ...) into Lisp
232     code. Lambda-List is a defmacro style lambda list."
233     (let ((fn-name (symbolicate "PRIMITIVE-TRANSLATE-" name))
234     (n-form (gensym))
235     (n-env (gensym)))
236 wlott 1.15 (multiple-value-bind
237     (body decls)
238 wlott 1.17 (lisp::parse-defmacro lambda-list n-form body name "%primitive"
239 wlott 1.16 :environment n-env
240 wlott 1.19 :error-fun 'convert-condition-into-compiler-error)
241 wlott 1.1 `(progn
242     (defun ,fn-name (,n-form)
243 ram 1.7 (let ((,n-env *lexical-environment*))
244 wlott 1.1 ,@decls
245 wlott 1.16 ,body))
246 wlott 1.1 (setf (gethash ',name *primitive-translators*) ',fn-name)))))
247    
248    
249     ;;;; Lambda-list parsing utilities:
250     ;;;
251     ;;; IR1 transforms, optimizers and type inferencers need to be able to parse
252     ;;; the IR1 representation of a function call using a standard function
253     ;;; lambda-list.
254    
255    
256 ram 1.22 (eval-when (compile load eval)
257 wlott 1.1
258     ;;; Parse-Deftransform -- Internal
259     ;;;
260     ;;; Given a deftransform style lambda-list, generate code that parses the
261     ;;; arguments of a combination with respect to that lambda-list. Body is the
262     ;;; the list of forms which are to be evaluated within the bindings. Args is
263     ;;; the variable that holds list of argument continuations. Error-Form is a
264     ;;; form which is evaluated when the syntax of the supplied arguments is
265     ;;; incorrect or a non-constant argument keyword is supplied. Defaults and
266     ;;; other gunk are ignored. The second value is a list of all the arguments
267     ;;; bound. We make the variables IGNORABLE so that we don't have to manually
268     ;;; declare them Ignore if their only purpose is to make the syntax work.
269     ;;;
270     (proclaim '(function parse-deftransform (list list symbol t) list))
271     (defun parse-deftransform (lambda-list body args error-form)
272     (multiple-value-bind (req opt restp rest keyp keys allowp)
273     (parse-lambda-list lambda-list)
274     (let* ((min-args (length req))
275     (max-args (+ min-args (length opt)))
276     (n-keys (gensym)))
277     (collect ((binds)
278     (vars)
279     (pos 0 +)
280     (keywords))
281     (dolist (arg req)
282     (vars arg)
283     (binds `(,arg (nth ,(pos) ,args)))
284     (pos 1))
285    
286     (dolist (arg opt)
287     (let ((var (if (atom arg) arg (first arg))))
288     (vars var)
289     (binds `(,var (nth ,(pos) ,args)))
290     (pos 1)))
291    
292     (when restp
293     (vars rest)
294     (binds `(,rest (nthcdr ,(pos) ,args))))
295    
296     (dolist (spec keys)
297     (if (or (atom spec) (atom (first spec)))
298     (let* ((var (if (atom spec) spec (first spec)))
299     (key (intern (symbol-name var) "KEYWORD")))
300     (vars var)
301     (binds `(,var (find-keyword-continuation ,n-keys ,key)))
302     (keywords key))
303     (let* ((head (first spec))
304     (var (second head))
305     (key (first head)))
306     (vars var)
307     (binds `(,var (find-keyword-continuation ,n-keys ,key)))
308     (keywords key))))
309    
310     (let ((n-length (gensym))
311     (limited-legal (not (or restp keyp))))
312     (values
313     `(let ((,n-length (length ,args))
314     ,@(when keyp `((,n-keys (nthcdr ,(pos) ,args)))))
315     (unless (and
316     ,(if limited-legal
317     `(<= ,min-args ,n-length ,max-args)
318     `(<= ,min-args ,n-length))
319     ,@(when keyp
320     (if allowp
321     `((check-keywords-constant ,n-keys))
322     `((check-transform-keys ,n-keys ',(keywords))))))
323     ,error-form)
324     (let ,(binds)
325     ;;; ### Bootstrap hack...
326     #+new-compiler
327     (declare (ignorable ,@(vars)))
328     #-new-compiler
329     (progn ,@(vars))
330     ,@body))
331     (vars)))))))
332    
333     ); Eval-When (Compile Load Eval)
334    
335    
336     ;;;; Deftransform:
337    
338     ;;; Deftransform -- Interface
339     ;;;
340     ;;; Parse the lambda-list and generate code to test the policy and
341     ;;; automatically create the result lambda.
342     ;;;
343 wlott 1.29 (defmacro deftransform (name (lambda-list &optional (arg-types '*)
344     (result-type '*)
345 ram 1.21 &key result policy node defun-only
346 ram 1.34 eval-name important (when :native))
347 ram 1.25 &body (body decls doc))
348 wlott 1.1 "Deftransform Name (Lambda-List [Arg-Types] [Result-Type] {Key Value}*)
349 ram 1.25 Declaration* [Doc-String] Form*
350 wlott 1.1 Define an IR1 transformation for Name. An IR1 transformation computes a
351     lambda that replaces the function variable reference for the call. A
352     transform may pass (decide not to transform the call) by calling the Give-Up
353     function. Lambda-List both determines how the current call is parsed and
354     specifies the Lambda-List for the resulting lambda.
355    
356     We parse the call and bind each of the lambda-list variables to the
357     continuation which represents the value of the argument. When parsing the
358     call, we ignore the defaults, and always bind the variables for unsupplied
359     arguments to NIL. If a required argument is missing, an unknown keyword is
360     supplied, or an argument keyword is not a constant, then the transform
361     automatically passes. The Declarations apply to the bindings made by
362     Deftransform at transformation time, rather than to the variables of the
363     resulting lambda. Bound-but-not-referenced warnings are suppressed for the
364 ram 1.25 lambda-list variables. The Doc-String is used when printing efficiency notes
365     about the defined transform.
366 wlott 1.1
367     Normally, the body evaluates to a form which becomes the body of an
368     automatically constructed lambda. We make Lambda-List the lambda-list for
369     the lambda, and automatically insert declarations of the argument and result
370     types. If the second value of the body is non-null, then it is a list of
371     declarations which are to be inserted at the head of the lambda. Automatic
372     lambda generation may be inhibited by explicitly returning a lambda from the
373     body.
374    
375     The Arg-Types and Result-Type are used to create a function type which the
376     call must satisfy before transformation is attempted. The function type
377     specifier is constructed by wrapping (FUNCTION ...) around these values, so
378     the lack of a restriction may be specified by omitting the argument or
379     supplying *. The argument syntax specified in the Arg-Types need not be the
380     same as that in the Lambda-List, but the transform will never happen if
381     the syntaxes can't be satisfied simultaneously. If there is an existing
382     transform for the same function that has the same type, then it is replaced
383     with the new definition.
384    
385     These are the legal keyword options:
386     :Result - A variable which is bound to the result continuation.
387     :Node - A variable which is bound to the combination node for the call.
388     :Policy - A form which is supplied to the Policy macro to determine whether
389     this transformation is appropriate. If the result is false, then
390     the transform automatically passes.
391 ram 1.21 :Eval-Name
392 ram 1.26 - The name and argument/result types are actually forms to be
393     evaluated. Useful for getting closures that transform similar
394     functions.
395 wlott 1.1 :Defun-Only
396     - Don't actually instantiate a transform, instead just DEFUN
397     Name with the specified transform definition function. This may
398 wlott 1.29 be later instantiated with %Deftransform.
399     :Important
400     - If supplied and non-NIL, note this transform as ``important,''
401     which means effeciency notes will be generated when this
402 ram 1.34 transform fails even if brevity=speed (but not if brevity>speed)
403     :When {:Native | :Byte | :Both}
404     - Indicates whether this transform applies to native code,
405     byte-code or both (default :native.)"
406 wlott 1.1
407 ram 1.21 (when (and eval-name defun-only)
408     (error "Can't specify both DEFUN-ONLY and EVAL-NAME."))
409 wlott 1.1 (let ((n-args (gensym))
410     (n-node (or node (gensym)))
411     (n-decls (gensym))
412 ram 1.25 (n-lambda (gensym))
413     (body `(,@decls ,@body)))
414 wlott 1.1 (multiple-value-bind (parsed-form vars)
415     (parse-deftransform
416     lambda-list
417     (if policy
418 ram 1.11 `((unless (policy ,n-node ,policy) (give-up))
419 wlott 1.1 ,@body)
420     body)
421     n-args '(give-up))
422     (let ((stuff
423     `((,n-node)
424     (let* ((,n-args (basic-combination-args ,n-node))
425     ,@(when result
426     `((,result (node-cont ,n-node)))))
427     (multiple-value-bind (,n-lambda ,n-decls)
428     ,parsed-form
429     (if (and (consp ,n-lambda) (eq (car ,n-lambda) 'lambda))
430     ,n-lambda
431     `(lambda ,',lambda-list
432     (declare (ignorable ,@',vars))
433     ,@,n-decls
434     ,,n-lambda)))))))
435     (if defun-only
436 ram 1.25 `(defun ,name ,@(when doc `(,doc)) ,@stuff)
437 wlott 1.1 `(%deftransform
438 ram 1.21 ,(if eval-name name `',name)
439 ram 1.26 ,(if eval-name
440     ``(function ,,arg-types ,,result-type)
441     `'(function ,arg-types ,result-type))
442 ram 1.25 #'(lambda ,@stuff)
443 wlott 1.29 ,doc
444 ram 1.34 ,(if important t nil)
445     ,when))))))
446 wlott 1.1
447     ;;;; Defknown, Defoptimizer:
448    
449     ;;; Defknown -- Interface
450     ;;;
451     ;;; This macro should be the way that all implementation independent
452     ;;; information about functions is made known to the compiler.
453     ;;;
454     (defmacro defknown (name arg-types result-type &optional (attributes '(any))
455     &rest keys)
456     "Defknown Name Arg-Types Result-Type [Attributes] {Key Value}*
457     Declare the function Name to be a known function. We construct a type
458     specifier for the function by wrapping (FUNCTION ...) around the Arg-Types
459     and Result-Type. Attributes is a an unevaluated list of the boolean
460     attributes that the function has. These attributes are meaningful here:
461     call
462     May call functions that are passed as arguments. In order to determine
463     what other effects are present, we must find the effects of all arguments
464     that may be functions.
465    
466     unsafe
467     May incorporate arguments in the result or somehow pass them upward.
468    
469     unwind
470     May fail to return during correct execution. Errors are O.K.
471    
472     any
473     The (default) worst case. Includes all the other bad things, plus any
474     other possible bad thing.
475    
476     foldable
477     May be constant-folded. The function has no side effects, but may be
478     affected by side effects on the arguments. e.g. SVREF, MAPC.
479    
480     flushable
481     May be eliminated if value is unused. The function has no side effects
482     except possibly CONS. If a function is defined to signal errors, then
483     it is not flushable even if it is movable or foldable.
484    
485     movable
486     May be moved with impunity. Has no side effects except possibly CONS,
487     and is affected only by its arguments.
488    
489     predicate
490     A true predicate likely to be open-coded. This is a hint to IR1
491     conversion that it should ensure calls always appear as an IF test.
492     Not usually specified to Defknown, since this is implementation
493     dependent, and is usually automatically set by the Define-VOP
494     :Conditional option.
495    
496     Name may also be a list of names, in which case the same information is given
497     to all the names. The keywords specify the initial values for various
498     optimizers that the function might have."
499     (when (and (intersection attributes '(any call unwind))
500     (intersection attributes '(movable)))
501     (error "Function cannot have both good and bad attributes: ~S" attributes))
502    
503     `(%defknown ',(if (and (consp name)
504     (not (eq (car name) 'setf)))
505     name
506     (list name))
507     '(function ,arg-types ,result-type)
508     (ir1-attributes ,@(if (member 'any attributes)
509     (union '(call unsafe unwind) attributes)
510     attributes))
511     ,@keys))
512    
513    
514     ;;; Defoptimizer -- Interface
515     ;;;
516     ;;; Create a function which parses combination args according to a
517     ;;; Lambda-List, optionally storing it in a function-info slot.
518     ;;;
519     (defmacro defoptimizer (what (lambda-list &optional (n-node (gensym))
520     &rest vars)
521     &body body)
522     "Defoptimizer (Function Kind) (Lambda-List [Node-Var] Var*)
523     Declaration* Form*
524     Define some Kind of optimizer for the named Function. Function must be a
525     known function. Lambda-List is used to parse the arguments to the
526     combination as in Deftransform. If the argument syntax is invalid or there
527     are non-constant keys, then we simply return NIL.
528    
529     The function is DEFUN'ed as Function-Kind-OPTIMIZER. Possible kinds are
530     DERIVE-TYPE, OPTIMIZER, LTN-ANNOTATE and IR2-CONVERT. If a symbol is
531     specified instead of a (Function Kind) list, then we just do a DEFUN with the
532     symbol as its name, and don't do anything with the definition. This is
533     useful for creating optimizers to be passed by name to DEFKNOWN.
534    
535     If supplied, Node-Var is bound to the combination node being optimized. If
536     additional Vars are supplied, then they are used as the rest of the optimizer
537     function's lambda-list. LTN-ANNOTATE methods are passed an additional POLICY
538     argument, and IR2-CONVERT methods are passed an additional IR2-BLOCK
539     argument."
540    
541     (let ((name (if (symbolp what) what
542     (symbolicate (first what) "-" (second what) "-OPTIMIZER"))))
543    
544     (let ((n-args (gensym)))
545     `(progn
546     (defun ,name (,n-node ,@vars)
547     (let ((,n-args (basic-combination-args ,n-node)))
548     ,(parse-deftransform lambda-list body n-args
549     `(return-from ,name nil))))
550     ,@(when (consp what)
551     `((setf (,(symbolicate "FUNCTION-INFO-" (second what))
552     (function-info-or-lose ',(first what)))
553     #',name)))))))
554    
555    
556     ;;;; IR groveling macros:
557    
558     ;;; Do-Blocks, Do-Blocks-Backwards -- Interface
559     ;;;
560     (defmacro do-blocks ((block-var component &optional ends result) &body body)
561     "Do-Blocks (Block-Var Component [Ends] [Result-Form]) {Declaration}* {Form}*
562     Iterate over the blocks in a component, binding Block-Var to each block in
563     turn. The value of Ends determines whether to iterate over dummy head and
564     tail blocks:
565     NIL -- Skip Head and Tail (the default)
566     :Head -- Do head but skip tail
567     :Tail -- Do tail but skip head
568     :Both -- Do both head and tail
569    
570     If supplied, Result-Form is the value to return."
571     (unless (member ends '(nil :head :tail :both))
572     (error "Losing Ends value: ~S." ends))
573     (let ((n-component (gensym))
574     (n-tail (gensym)))
575     `(let* ((,n-component ,component)
576     (,n-tail ,(if (member ends '(:both :tail))
577     nil
578     `(component-tail ,n-component))))
579     (do ((,block-var ,(if (member ends '(:both :head))
580     `(component-head ,n-component)
581     `(block-next (component-head ,n-component)))
582     (block-next ,block-var)))
583     ((eq ,block-var ,n-tail) ,result)
584     ,@body))))
585     ;;;
586     (defmacro do-blocks-backwards ((block-var component &optional ends result) &body body)
587     "Do-Blocks-Backwards (Block-Var Component [Ends] [Result-Form]) {Declaration}* {Form}*
588     Like Do-Blocks, only iterate over the blocks in reverse order."
589     (unless (member ends '(nil :head :tail :both))
590     (error "Losing Ends value: ~S." ends))
591     (let ((n-component (gensym))
592     (n-head (gensym)))
593     `(let* ((,n-component ,component)
594     (,n-head ,(if (member ends '(:both :head))
595     nil
596     `(component-head ,n-component))))
597     (do ((,block-var ,(if (member ends '(:both :tail))
598     `(component-tail ,n-component)
599     `(block-prev (component-tail ,n-component)))
600     (block-prev ,block-var)))
601     ((eq ,block-var ,n-head) ,result)
602     ,@body))))
603    
604    
605     ;;; Do-Uses -- Interface
606     ;;;
607     ;;; Could change it not to replicate the code someday perhaps...
608     ;;;
609     (defmacro do-uses ((node-var continuation &optional result) &body body)
610     "Do-Uses (Node-Var Continuation [Result]) {Declaration}* {Form}*
611     Iterate over the uses of Continuation, binding Node to each one succesively."
612     (once-only ((n-cont continuation))
613     `(ecase (continuation-kind ,n-cont)
614     (:unused)
615     (:inside-block
616     (block nil
617     (let ((,node-var (continuation-use ,n-cont)))
618     ,@body
619     ,result)))
620     ((:block-start :deleted-block-start)
621     (dolist (,node-var (block-start-uses (continuation-block ,n-cont))
622     ,result)
623     ,@body)))))
624    
625    
626     ;;; Do-Nodes, Do-Nodes-Backwards -- Interface
627     ;;;
628     ;;; In the forward case, we terminate on Last-Cont so that we don't have to
629     ;;; worry about our termination condition being changed when new code is added
630 ram 1.20 ;;; during the iteration. In the backward case, we do NODE-PREV before
631 wlott 1.1 ;;; evaluating the body so that we can keep going when the current node is
632     ;;; deleted.
633     ;;;
634 ram 1.20 ;;; When Restart-P is supplied to DO-NODES, we start iterating over again at
635     ;;; the beginning of the block when we run into a continuation whose block
636     ;;; differs from the one we are trying to iterate over, either beacuse the
637     ;;; block was split, or because a node was deleted out from under us (hence its
638     ;;; block is NIL.) If the block start is deleted, we just punt. With
639     ;;; Restart-P, we are also more careful about termination, re-indirecting the
640     ;;; BLOCK-LAST each time.
641     ;;;
642     (defmacro do-nodes ((node-var cont-var block &key restart-p) &body body)
643     "Do-Nodes (Node-Var Cont-Var Block {Key Value}*) {Declaration}* {Form}*
644 wlott 1.1 Iterate over the nodes in Block, binding Node-Var to the each node and
645 ram 1.20 Cont-Var to the node's Cont. The only keyword option is Restart-P, which
646     causes iteration to be restarted when a node is deleted out from under us (if
647     not supplied, this is an error.)"
648 wlott 1.1 (let ((n-block (gensym))
649 ram 1.20 (n-last-cont (gensym)))
650 wlott 1.1 `(let* ((,n-block ,block)
651 ram 1.20 ,@(unless restart-p
652     `((,n-last-cont (node-cont (block-last ,n-block))))))
653 wlott 1.1 (do* ((,node-var (continuation-next (block-start ,n-block))
654 ram 1.20 ,(if restart-p
655     `(cond
656     ((eq (continuation-block ,cont-var) ,n-block)
657     (assert (continuation-next ,cont-var))
658     (continuation-next ,cont-var))
659     (t
660     (let ((start (block-start ,n-block)))
661     (unless (eq (continuation-kind start)
662     :block-start)
663     (return nil))
664     (continuation-next start))))
665     `(continuation-next ,cont-var)))
666 wlott 1.1 (,cont-var (node-cont ,node-var) (node-cont ,node-var)))
667     (())
668     ,@body
669 ram 1.20 (when ,(if restart-p
670     `(eq ,node-var (block-last ,n-block))
671     `(eq ,cont-var ,n-last-cont))
672     (return nil))))))
673 wlott 1.1 ;;;
674 ram 1.20 (defmacro do-nodes-backwards ((node-var cont-var block) &body body)
675     "Do-Nodes-Backwards (Node-Var Cont-Var Block) {Declaration}* {Form}*
676 wlott 1.1 Like Do-Nodes, only iterates in reverse order."
677     (let ((n-block (gensym))
678     (n-start (gensym))
679     (n-last (gensym))
680     (n-next (gensym)))
681     `(let* ((,n-block ,block)
682     (,n-start (block-start ,n-block))
683     (,n-last (block-last ,n-block)))
684     (do* ((,cont-var (node-cont ,n-last) ,n-next)
685     (,node-var ,n-last (continuation-use ,cont-var))
686     (,n-next (node-prev ,node-var) (node-prev ,node-var)))
687     (())
688     ,@body
689     (when (eq ,n-next ,n-start)
690 ram 1.20 (return nil))))))
691 wlott 1.1
692    
693     ;;; With-IR1-Environment -- Interface
694     ;;;
695     ;;; The lexical environment is presumably already null...
696     ;;;
697     (defmacro with-ir1-environment (node &rest forms)
698     "With-IR1-Environment Node Form*
699     Bind the IR1 context variables so that IR1 conversion can be done after the
700 ram 1.7 main conversion pass has finished."
701     (let ((n-node (gensym)))
702 wlott 1.1 `(let* ((,n-node ,node)
703 ram 1.7 (*current-component* (block-component (node-block ,n-node)))
704     (*lexical-environment* (node-lexenv ,n-node))
705     (*current-path* (node-source-path ,n-node)))
706 wlott 1.1 ,@forms)))
707    
708    
709     ;;; WITH-IR1-NAMESPACE -- Interface
710     ;;;
711     ;;; Bind the hashtables used for keeping track of global variables,
712 ram 1.36 ;;; functions, &c. Also establish condition handlers.
713 wlott 1.1 ;;;
714     (defmacro with-ir1-namespace (&body forms)
715     `(let ((*free-variables* (make-hash-table :test #'eq))
716     (*free-functions* (make-hash-table :test #'equal))
717     (*constants* (make-hash-table :test #'equal))
718 wlott 1.39 (*coalesce-constants* t)
719 ram 1.32 (*source-paths* (make-hash-table :test #'eq)))
720 ram 1.36 (handler-bind ((compiler-error #'compiler-error-handler)
721     (style-warning #'compiler-style-warning-handler)
722     (warning #'compiler-warning-handler))
723     ,@forms)))
724 wlott 1.1
725 ram 1.7
726     ;;; LEXENV-FIND -- Interface
727     ;;;
728     (defmacro lexenv-find (name slot &key test)
729     "LEXENV-FIND Name Slot {Key Value}*
730     Look up Name in the lexical environment namespace designated by Slot,
731     returning the <value, T>, or <NIL, NIL> if no entry. The :TEST keyword
732     may be used to determine the name equality predicate."
733     (once-only ((n-res `(assoc ,name (,(symbolicate "LEXENV-" slot)
734     *lexical-environment*)
735 ram 1.37 :test ,(or test '#'eq))))
736 ram 1.7 `(if ,n-res
737     (values (cdr ,n-res) t)
738     (values nil nil))))
739    
740 wlott 1.1
741     ;;;; The Defprinter macro:
742    
743     (defvar *defprint-pretty* nil
744     "If true, defprinter print functions print each slot on a separate line.")
745    
746    
747     ;;; Defprinter-Prin1, Defprinter-Princ -- Internal
748     ;;;
749     ;;; These functions are called by the expansion of the Defprinter
750     ;;; macro to do the actual printing.
751     ;;;
752 ram 1.20 (proclaim '(ftype (function (symbol t stream &optional t) void)
753 wlott 1.1 defprinter-prin1 defprinter-princ))
754 wlott 1.14 (defun defprinter-prin1 (name value stream &optional indent)
755     (declare (ignore indent))
756     (write-string " " stream)
757     (when *print-pretty*
758 wlott 1.27 (pprint-newline :linear stream))
759 wlott 1.14 (princ name stream)
760     (write-string "= " stream)
761     (prin1 value stream))
762 wlott 1.1 ;;;
763 wlott 1.14 (defun defprinter-princ (name value stream &optional indent)
764 wlott 1.1 (declare (ignore indent))
765 wlott 1.14 (write-string " " stream)
766     (when *print-pretty*
767 wlott 1.27 (pprint-newline :linear stream))
768 wlott 1.14 (princ name stream)
769     (write-string "= " stream)
770     (princ value stream))
771 wlott 1.1
772     (defmacro defprinter (name &rest slots)
773     "Defprinter Name Slot-Desc*
774     Define some kind of reasonable defstruct structure-print function. Name
775     is the name of the structure. We define a function %PRINT-name which
776     prints the slots in the structure in the way described by the Slot-Descs.
777     Each Slot-Desc can be a slot name, indicating that the slot should simply
778     be printed. A Slot-Desc may also be a list of a slot name and other stuff.
779     The other stuff is composed of keywords followed by expressions. The
780     expressions are evaluated with the variable which is the slot name bound
781     to the value of the slot. These keywords are defined:
782    
783     :PRIN1 Print the value of the expression instead of the slot value.
784     :PRINC Like :PRIN1, only princ the value
785     :TEST Only print something if the test is true.
786    
787     If no printing thing is specified then the slot value is printed as PRIN1.
788    
789     The structure being printed is bound to Structure and the stream is bound to
790     Stream."
791    
792 wlott 1.14 (flet ((sref (slot) `(,(symbolicate name "-" slot) structure)))
793     (collect ((prints))
794     (dolist (slot slots)
795     (if (atom slot)
796     (prints `(defprinter-prin1 ',slot ,(sref slot) stream))
797     (let ((sname (first slot))
798     (test t))
799     (collect ((stuff))
800     (do ((option (rest slot) (cddr option)))
801     ((null option)
802     (prints
803     `(let ((,sname ,(sref sname)))
804     (when ,test
805     ,@(or (stuff)
806     `((defprinter-prin1 ',sname ,sname
807     stream)))))))
808     (case (first option)
809     (:prin1
810     (stuff `(defprinter-prin1 ',sname ,(second option)
811     stream)))
812     (:princ
813     (stuff `(defprinter-princ ',sname ,(second option)
814     stream)))
815     (:test (setq test (second option)))
816     (t
817     (error "Losing Defprinter option: ~S."
818     (first option)))))))))
819    
820 wlott 1.27 `(defun ,(symbolicate "%PRINT-" name) (structure stream depth)
821 wlott 1.29 (flet ((do-prints (stream)
822 wlott 1.30 (declare (ignorable stream))
823 wlott 1.27 ,@(prints)))
824 wlott 1.31 (cond (*print-readably*
825     (error "~S cannot be printed readably." structure))
826     ((and *print-level* (>= depth *print-level*))
827     (format stream "#<~S ~X>"
828     ',name
829     (get-lisp-obj-address structure)))
830     (*print-pretty*
831 ram 1.28 (pprint-logical-block (stream nil :prefix "#<" :suffix ">")
832 wlott 1.29 (pprint-indent :current 2 stream)
833 ram 1.28 (prin1 ',name stream)
834     (write-char #\space stream)
835     (let ((*print-base* 16)
836     (*print-radix* t))
837     (prin1 (get-lisp-obj-address structure) stream))
838 wlott 1.29 (do-prints stream)))
839 wlott 1.27 (t
840 wlott 1.31 (descend-into (stream)
841     (format stream "#<~S ~X"
842     ',name
843     (get-lisp-obj-address structure))
844     (do-prints stream)
845     (format stream ">")))))
846 wlott 1.27 nil))))
847 wlott 1.1
848    
849     ;;;; Boolean attribute utilities:
850     ;;;
851     ;;; We need to maintain various sets of boolean attributes for known
852     ;;; functions and VOPs. To save space and allow for quick set operations, we
853     ;;; represent them as bits in a fixnum.
854     ;;;
855    
856     (deftype attributes () 'fixnum)
857    
858 ram 1.7 (eval-when (compile load eval)
859 wlott 1.1 ;;; Compute-Attribute-Mask -- Internal
860     ;;;
861     ;;; Given a list of attribute names and an alist that translates them to
862     ;;; masks, return the OR of the masks.
863     ;;;
864     (defun compute-attribute-mask (names alist)
865     (collect ((res 0 logior))
866     (dolist (name names)
867     (let ((mask (cdr (assoc name alist))))
868     (unless mask
869     (error "Unknown attribute name: ~S." name))
870     (res mask)))
871     (res)))
872    
873     ); Eval-When (Compile Load Eval)
874    
875     ;;; Def-Boolean-Attribute -- Interface
876     ;;;
877     ;;; Parse the specification and generate some accessor macros.
878     ;;;
879     (defmacro def-boolean-attribute (name &rest attribute-names)
880     "Def-Boolean-Attribute Name Attribute-Name*
881     Define a new class of boolean attributes, with the attributes havin the
882     specified Attribute-Names. Name is the name of the class, which is used to
883     generate some macros to manipulate sets of the attributes:
884    
885     NAME-attributep attributes attribute-name*
886     Return true if one of the named attributes is present, false otherwise.
887 ram 1.7 When set with SETF, updates the place Attributes setting or clearing the
888     specified attributes.
889 wlott 1.1
890     NAME-attributes attribute-name*
891     Return a set of the named attributes."
892    
893 ram 1.7 (let ((const-name (symbolicate name "-ATTRIBUTE-TRANSLATIONS"))
894     (test-name (symbolicate name "-ATTRIBUTEP")))
895 wlott 1.1 (collect ((alist))
896     (do ((mask 1 (ash mask 1))
897     (names attribute-names (cdr names)))
898     ((null names))
899     (alist (cons (car names) mask)))
900    
901     `(progn
902 ram 1.7 (eval-when (compile load eval)
903     (defconstant ,const-name ',(alist)))
904    
905     (defmacro ,test-name (attributes &rest attribute-names)
906     "Automagically generated boolean attribute test function. See
907     Def-Boolean-Attribute."
908     `(logtest ,(compute-attribute-mask attribute-names ,const-name)
909     (the attributes ,attributes)))
910 wlott 1.1
911 ram 1.7 (define-setf-method ,test-name (place &rest attributes
912     &environment env)
913    
914     "Automagically generated boolean attribute setter. See
915     Def-Boolean-Attribute."
916     (multiple-value-bind (temps values stores set get)
917 ram 1.24 (get-setf-method place env)
918 ram 1.7 (let ((newval (gensym))
919     (n-place (gensym))
920     (mask (compute-attribute-mask attributes ,const-name)))
921     (values `(,@temps ,n-place)
922     `(,@values ,get)
923     `(,newval)
924     `(let ((,(first stores)
925     (if ,newval
926     (logior ,n-place ,mask)
927     (logand ,n-place ,(lognot mask)))))
928     ,set
929     ,newval)
930     `(,',test-name ,n-place ,@attributes)))))
931    
932     (defmacro ,(symbolicate name "-ATTRIBUTES") (&rest attribute-names)
933     "Automagically generated boolean attribute creation function. See
934     Def-Boolean-Attribute."
935     (compute-attribute-mask attribute-names ,const-name))))))
936 wlott 1.1
937    
938     ;;; Attributes-Union, Attributes-Intersection, Attributes= -- Interface
939     ;;;
940     ;;; And now for some gratuitous pseudo-abstraction...
941     ;;;
942     (defmacro attributes-union (&rest attributes)
943     "Returns the union of all the sets of boolean attributes which are its
944     arguments."
945     `(the attributes
946     (logior ,@(mapcar #'(lambda (x) `(the attributes ,x)) attributes))))
947     ;;;
948     (defmacro attributes-intersection (&rest attributes)
949     "Returns the intersection of all the sets of boolean attributes which are its
950     arguments."
951     `(the attributes
952     (logand ,@(mapcar #'(lambda (x) `(the attributes ,x)) attributes))))
953     ;;;
954     (proclaim '(inline attributes=))
955     (proclaim '(function attributes= (attributes attributes) boolean))
956     (defun attributes= (attr1 attr2)
957     "Returns true if the attributes present in Attr1 are indentical to those in
958     Attr2."
959     (eql attr1 attr2))
960    
961    
962     ;;;; The Event statistics/trace utility:
963    
964 ram 1.22 (eval-when (compile load eval)
965 wlott 1.1
966     (defstruct event-info
967     ;;
968     ;; The name of this event.
969 ram 1.18 (name (required-argument) :type symbol)
970 wlott 1.1 ;;
971     ;; The string rescribing this event.
972 ram 1.18 (description (required-argument) :type string)
973 wlott 1.1 ;;
974     ;; The name of the variable we stash this in.
975 ram 1.18 (var (required-argument) :type symbol)
976 wlott 1.1 ;;
977     ;; The number of times this event has happened.
978     (count 0 :type fixnum)
979     ;;
980     ;; The level of significance of this event.
981 ram 1.18 (level (required-argument) :type unsigned-byte)
982 wlott 1.1 ;;
983     ;; If true, a function that gets called with the node that the event happened
984     ;; to.
985     (action nil :type (or function null)))
986    
987     ;;; A hashtable from event names to event-info structures.
988     ;;;
989     (defvar *event-info* (make-hash-table :test #'eq))
990    
991    
992     ;;; Event-Info-Or-Lose -- Internal
993     ;;;
994     ;;; Return the event info for Name or die trying.
995     ;;;
996     (proclaim '(function event-info-or-lose (t) event-info))
997     (defun event-info-or-lose (name)
998     (let ((res (gethash name *event-info*)))
999     (unless res
1000     (error "~S is not the name of an event." name))
1001     res))
1002    
1003     ); Eval-When (Compile Load Eval)
1004    
1005    
1006     ;;; Event-Count, Event-Action, Event-Level -- Interface
1007     ;;;
1008     (proclaim '(function event-count (symbol) fixnum))
1009     (defun event-count (name)
1010     "Return the number of times that Event has happened."
1011     (event-info-count (event-info-or-lose name)))
1012     ;;;
1013     (proclaim '(function event-action (symbol) (or function null)))
1014     (defun event-action (name)
1015     "Return the function that is called when Event happens. If this is null,
1016     there is no action. The function is passed the node to which the event
1017     happened, or NIL if there is no relevant node. This may be set with SETF."
1018     (event-info-action (event-info-or-lose name)))
1019     ;;;
1020     (proclaim '(function %set-event-action (symbol (or function null)) (or function null)))
1021     (defun %set-event-action (name new-value)
1022     (setf (event-info-action (event-info-or-lose name))
1023     new-value))
1024     ;;;
1025     (defsetf event-action %set-event-action)
1026     ;;;
1027     (proclaim '(function event-level (symbol) unsigned-byte))
1028     (defun event-level (name)
1029     "Return the non-negative integer which represents the level of significance
1030     of the event Name. This is used to determine whether to print a message when
1031     the event happens. This may be set with SETF."
1032     (event-info-level (event-info-or-lose name)))
1033     ;;;
1034     (proclaim '(function %set-event-level (symbol unsigned-byte) unsigned-byte))
1035     (defun %set-event-level (name new-value)
1036     (setf (event-info-level (event-info-or-lose name))
1037     new-value))
1038     ;;;
1039     (defsetf event-level %set-event-level)
1040    
1041    
1042     ;;; Defevent -- Interface
1043     ;;;
1044     ;;; Make an event-info structure and stash it in a variable so we can get at
1045     ;;; it quickly.
1046     ;;;
1047     (defmacro defevent (name description &optional (level 0))
1048     "Defevent Name Description
1049     Define a new kind of event. Name is a symbol which names the event and
1050     Description is a string which describes the event. Level (default 0) is the
1051     level of significance associated with this event; it is used to determine
1052     whether to print a Note when the event happens."
1053     (let ((var-name (symbolicate "*" name "-EVENT-INFO*")))
1054     `(eval-when (compile load eval)
1055     (defvar ,var-name
1056     (make-event-info :name ',name :description ',description :var ',var-name
1057     :level ,level))
1058     (setf (gethash ',name *event-info*) ,var-name)
1059     ',name)))
1060    
1061     (proclaim '(type unsigned-byte *event-note-threshold*))
1062     (defvar *event-note-threshold* 1
1063     "This variable is a non-negative integer specifying the lowest level of
1064     event that will print a Note when it occurs.")
1065    
1066     ;;; Event -- Interface
1067     ;;;
1068     ;;; Increment the counter and do any action. Mumble about the event if
1069     ;;; policy indicates.
1070     ;;;
1071     (defmacro event (name &optional node)
1072     "Event Name Node
1073     Note that the event with the specified Name has happened. Node is evaluated
1074     to determine the node to which the event happened."
1075     `(%event ,(event-info-var (event-info-or-lose name)) ,node))
1076    
1077    
1078     ;;; Event-Statistics, Clear-Statistics -- Interface
1079     ;;;
1080     (proclaim '(function event-statistics (&optional unsigned-byte stream) void))
1081     (defun event-statistics (&optional (min-count 1) (stream *standard-output*))
1082     "Print a listing of events and their counts, sorted by the count. Events
1083     that happened fewer than Min-Count times will not be printed. Stream is the
1084     stream to write to."
1085     (collect ((info))
1086     (maphash #'(lambda (k v)
1087     (declare (ignore k))
1088     (when (>= (event-info-count v) min-count)
1089     (info v)))
1090     *event-info*)
1091     (dolist (event (sort (info) #'> :key #'event-info-count))
1092     (format stream "~6D: ~A~%" (event-info-count event)
1093     (event-info-description event)))
1094     (values)))
1095     ;;;
1096     (proclaim '(function clear-statistics () void))
1097     (defun clear-statistics ()
1098     (maphash #'(lambda (k v)
1099     (declare (ignore k))
1100     (setf (event-info-count v) 0))
1101     *event-info*)
1102     (values))
1103 ram 1.40
1104    
1105     ;;;; Generic list (?) functions:
1106    
1107     (proclaim '(inline find-in position-in map-in))
1108    
1109     ;;; Find-In -- Interface
1110     ;;;
1111     (defun find-in (next element list &key (key #'identity)
1112     (test #'eql test-p) (test-not nil not-p))
1113     "Find Element in a null-terminated List linked by the accessor function
1114     Next. Key, Test and Test-Not are the same as for generic sequence
1115     functions."
1116     (when (and test-p not-p)
1117     (error "Silly to supply both :Test and :Test-Not."))
1118     (if not-p
1119     (do ((current list (funcall next current)))
1120     ((null current) nil)
1121     (unless (funcall test-not (funcall key current) element)
1122     (return current)))
1123     (do ((current list (funcall next current)))
1124     ((null current) nil)
1125     (when (funcall test (funcall key current) element)
1126     (return current)))))
1127    
1128     ;;; Position-In -- Interface
1129     ;;;
1130     (defun position-in (next element list &key (key #'identity)
1131     (test #'eql test-p) (test-not nil not-p))
1132     "Return the position of Element (or NIL if absent) in a null-terminated List
1133     linked by the accessor function Next. Key, Test and Test-Not are the same as
1134     for generic sequence functions."
1135     (when (and test-p not-p)
1136     (error "Silly to supply both :Test and :Test-Not."))
1137     (if not-p
1138     (do ((current list (funcall next current))
1139     (i 0 (1+ i)))
1140     ((null current) nil)
1141     (unless (funcall test-not (funcall key current) element)
1142     (return i)))
1143     (do ((current list (funcall next current))
1144     (i 0 (1+ i)))
1145     ((null current) nil)
1146     (when (funcall test (funcall key current) element)
1147     (return i)))))
1148    
1149    
1150     ;;; Map-In -- Interface
1151     ;;;
1152     (defun map-in (next function list)
1153     "Map Function over the elements in a null-terminated List linked by the
1154     accessor function Next, returning a list of the results."
1155     (collect ((res))
1156     (do ((current list (funcall next current)))
1157     ((null current))
1158     (res (funcall function current)))
1159     (res)))
1160    
1161    
1162     ;;; Deletef-In -- Interface
1163     ;;;
1164     (defmacro deletef-in (next place item &environment env)
1165     "Deletef-In Next Place Item
1166     Delete Item from a null-terminated list linked by the accessor function Next
1167     that is stored in Place. Item must appear exactly once in the list."
1168     (multiple-value-bind
1169     (temps vals stores store access)
1170     (get-setf-method place env)
1171     (let ((n-item (gensym))
1172     (n-place (gensym))
1173     (n-current (gensym))
1174     (n-prev (gensym)))
1175     `(let* (,@(mapcar #'list temps vals)
1176     (,n-place ,access)
1177     (,n-item ,item))
1178     (if (eq ,n-place ,n-item)
1179     (let ((,(first stores) (,next ,n-place)))
1180     ,store)
1181     (do ((,n-prev ,n-place ,n-current)
1182     (,n-current (,next ,n-place)
1183     (,next ,n-current)))
1184     ((eq ,n-current ,n-item)
1185     (setf (,next ,n-prev)
1186     (,next ,n-current)))))
1187     (undefined-value)))))
1188    
1189    
1190     ;;; Push-In -- Interface
1191     ;;;
1192     (defmacro push-in (next item place &environment env)
1193     "Push Item onto a list linked by the accessor function Next that is stored in
1194     Place."
1195     (multiple-value-bind
1196     (temps vals stores store access)
1197     (get-setf-method place env)
1198     `(let (,@(mapcar #'list temps vals)
1199     (,(first stores) ,item))
1200     (setf (,next ,(first stores)) ,access)
1201     ,store
1202     (undefined-value))))
1203    
1204    
1205     ;;; EPOSITION -- Interface
1206     ;;;
1207     (defmacro eposition (&rest args)
1208     `(or (position ,@args)
1209     (error "Shouldn't happen?")))

  ViewVC Help
Powered by ViewVC 1.1.5