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

Contents of /src/compiler/macros.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5