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

Contents of /src/compiler/macros.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5