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

Contents of /src/compiler/macros.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.56.26.1 - (hide annotations)
Thu Feb 25 20:34:53 2010 UTC (4 years, 1 month ago) by rtoy
Branch: intl-2-branch
Changes since 1.56: +53 -52 lines
Restart internalization work.  This new branch starts with code from
the intl-branch on date 2010-02-12 18:00:00+0500.  This version works
and

LANG=en@piglatin bin/lisp

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

  ViewVC Help
Powered by ViewVC 1.1.5