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

Contents of /src/compiler/macros.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5