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

Contents of /src/compiler/macros.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5