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

Contents of /src/compiler/macros.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.55 - (hide annotations)
Thu Aug 4 16:00:04 2005 UTC (8 years, 8 months ago) by rtoy
Branch: MAIN
CVS Tags: double-double-array-base, double-double-init-sparc-2, double-double-base, snapshot-2007-09, snapshot-2007-08, snapshot-2007-05, snapshot-2006-11, snapshot-2006-10, double-double-init-sparc, snapshot-2006-12, snapshot-2007-01, snapshot-2007-02, release-19d, double-double-init-ppc, release-19c, release-19c-base, double-double-init-%make-sparc, snapshot-2007-03, snapshot-2007-04, snapshot-2007-07, snapshot-2007-06, double-double-array-checkpoint, double-double-reader-checkpoint-1, release-19d-base, double-double-irrat-end, release-19d-pre2, release-19d-pre1, double-double-init-checkpoint-1, double-double-reader-base, double-double-init-x86, snapshot-2005-11, double-double-sparc-checkpoint-1, snapshot-2005-10, snapshot-2005-12, release-19c-pre1, double-double-irrat-start, snapshot-2005-09, snapshot-2007-10, snapshot-2007-11, snapshot-2006-02, snapshot-2006-03, snapshot-2006-01, snapshot-2006-06, snapshot-2006-07, snapshot-2006-04, snapshot-2006-05, pre-telent-clx, snapshot-2006-08, snapshot-2006-09
Branch point for: double-double-reader-branch, double-double-array-branch, release-19d-branch, double-double-branch, release-19c-branch
Changes since 1.54: +12 -2 lines
Funcalling a special form signals an undefined-function error, but the
error object didn't include a value for the cell-error-name.

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

  ViewVC Help
Powered by ViewVC 1.1.5