1 ;;;; Utilities for verifying features of compiled code
3 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; While most of SBCL is derived from the CMU CL system, the test
7 ;;;; files (like this one) were written from scratch after the fork
10 ;;;; This software is in the public domain and is provided with
11 ;;;; absolutely no warranty. See the COPYING and CREDITS files for
12 ;;;; more information.
14 (defpackage :compiler-test-util
16 (:use :cl :sb-c :sb-kernel)
17 (:export #:assert-consing
19 #:compiler-derived-type
20 #:find-value-cell-values
27 (unless (fboundp 'compiler-derived-type)
28 (defknown compiler-derived-type (t) (values t t) (movable flushable unsafe))
29 (deftransform compiler-derived-type ((x) * * :node node)
30 (sb-c::delay-ir1-transform node :optimize)
31 `(values ',(type-specifier (sb-c::lvar-type x)) t))
32 (defun compiler-derived-type (x)
36 (defun find-value-cell-values (fun)
37 (let ((code (fun-code-header (%fun-fun fun))))
38 (loop for i from sb-vm::code-constants-offset below (get-header-data code)
39 for c = (code-header-ref code i)
40 when (= sb-vm::value-cell-header-widetag (widetag-of c))
41 collect (sb-vm::value-cell-ref c))))
43 (defun find-named-callees (fun &key (type t) (name nil namep))
44 (let ((code (sb-kernel:fun-code-header (sb-kernel:%fun-fun fun))))
45 (loop for i from sb-vm::code-constants-offset below (sb-kernel:get-header-data code)
46 for c = (sb-kernel:code-header-ref code i)
47 when (and (typep c 'sb-impl::fdefn)
48 (let ((fun (sb-impl::fdefn-fun c)))
51 (equal name (sb-impl::fdefn-name c))))))
52 collect (sb-impl::fdefn-fun c))))
54 (defun find-code-constants (fun &key (type t))
55 (let ((code (sb-kernel:fun-code-header (sb-kernel:%fun-fun fun))))
56 (loop for i from sb-vm::code-constants-offset below (sb-kernel:get-header-data code)
57 for c = (sb-kernel:code-header-ref code i)
61 (defun collect-consing-stats (thunk times)
62 (declare (type function thunk))
63 (declare (type fixnum times))
64 (let ((before (sb-ext:get-bytes-consed)))
67 (values before (sb-ext:get-bytes-consed))))
69 (defun check-consing (yes/no form thunk times)
70 (multiple-value-bind (before after)
71 (collect-consing-stats thunk times)
72 (let ((consed-bytes (- after before)))
73 (assert (funcall (if yes/no #'not #'identity)
74 ;; I do not know why we do this comparasion,
75 ;; the original code did, so I let it
76 ;; in. Perhaps to prevent losage on GC
77 ;; fluctuations, or something. --TCR.
78 (< consed-bytes times))
80 "~@<Expected the form ~
82 ~:[NOT to cons~;to cons~], yet running it for ~
83 ~D times resulted in the allocation of ~
84 ~D bytes~:[ (~,3F per run)~;~].~@:>"
85 form yes/no times consed-bytes
86 (zerop consed-bytes) (float (/ consed-bytes times))))
87 (values before after)))
89 (defparameter +times+ 10000)
91 (defmacro assert-no-consing (form &optional (times '+times+))
92 `(check-consing nil ',form (lambda () ,form) ,times))
94 (defmacro assert-consing (form &optional (times '+times+))
95 `(check-consing t ',form (lambda () ,form) ,times))
97 (defun file-compile (toplevel-forms &key load)
98 (let* ((lisp (merge-pathnames "file-compile-tmp.lisp"))
99 (fasl (compile-file-pathname lisp)))
102 (with-open-file (f lisp :direction :output)
103 (dolist (form toplevel-forms)
105 (multiple-value-bind (fasl warn fail) (compile-file lisp)
109 (ignore-errors (delete-file lisp))
110 (ignore-errors (delete-file fasl)))))