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

Contents of /src/compiler/macros.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5