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

Contents of /src/compiler/macros.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5