Debian Codegraph @

src/cmp/cmpclos.lsp

raw
1 ;;;; -*- Mode: Lisp; Syntax: Common-Lisp; indent-tabs-mode: nil; Package: C -*-
2 ;;;; vim: set filetype=lisp tabstop=8 shiftwidth=2 expandtab:
3
4 ;;;;
5 ;;;; CMPCLOS. CLOS related optimizations.
6
7 ;;;; Copyright (c) 2008. Juan Jose Garcia-Ripol
8 ;;;;
9 ;;;; See file 'LICENSE' for the copyright details.
10
11 (in-package "COMPILER")
12
13 ;;;
14 ;;; GENERIC OPTIMIZATION
15 ;;;
16
17 (defun maybe-optimize-generic-function (fname args)
18 (when (fboundp fname)
19 (let ((gf (fdefinition fname)))
20 (when (typep gf 'standard-generic-function *cmp-env*)
21 ;;(check-generic-function-args gf args)
22 (when (policy-inline-slot-access)
23 (maybe-optimize-slot-accessor fname gf args))))))
24
25 ;;;
26 ;;; PRECOMPUTE APPLICABLE METHODS
27 ;;;
28 ;;; Computes a list of methods that would apply given what we know
29 ;;; about their arguments. Since the types are not exact, we have to
30 ;;; use subtypep. We could speed this up if we could precompute the
31 ;;; classes for the c-args.
32 ;;;
33
34 (defun precompute-applicable-methods (methods c-args)
35 (flet ((applicable-method-p (m)
36 (loop for specializer in (clos:method-specializers m)
37 for arg in c-args
38 always (let ((arg-type (c1form-type arg)))
39 (subtypep arg-type
40 (if (consp specializer)
41 `(member ,(second specializer))
42 specializer)
43 *cmp-env*)))))
44 (delete-if-not #'applicable-method-p methods)))
45
46 ;;;
47 ;;; SLOT ACCESSORS
48 ;;;
49 ;;; The following functions deal with an ECL extension, which are
50 ;;; sealed slots. These slots have a fixed location which is
51 ;;; inherited by subclasses. They normally appear when you add the
52 ;;; option (:sealedp t) to a class definition.
53 ;;;
54 ;;; When ECL detects that you call an accessor to such a slot, it can
55 ;;; optimize the operation, using a direct access based on the
56 ;;; position of the slot. This optimization is only active when the
57 ;;; safety levels are low, because it prevents you from changing the
58 ;;; class hierarchy.
59 ;;;
60
61 (defun find-slot-accessors (gf)
62 (loop for method in (clos:generic-function-methods gf)
63 with readers = '()
64 with writers = '()
65 with reader-class = (find-class 'clos:standard-reader-method)
66 with writer-class = (find-class 'clos:standard-writer-method)
67 do (let ((method-class (class-of method)))
68 (cond ((si::subclassp method-class reader-class)
69 (push method readers))
70 ((si::subclassp method-class writer-class)
71 (push method writers))))
72 finally (return (values readers writers))))
73
74 (defun maybe-optimize-slot-accessor (fname gf args)
75 (multiple-value-bind (readers writers)
76 (find-slot-accessors gf)
77 ;(format t "~%;;; Found ~D readers and ~D writers for ~A" (length readers) (length writers) fname)
78 (cond ((and readers writers)
79 (cmpwarn "When analyzing generic function ~A found both slot reader and writer methods"
80 fname))
81 ((not (or readers writers))
82 nil)
83 ((/= (length args) (length (clos::generic-function-spec-list gf)))
84 (cmpwarn "Too many arguments for generic function ~A" fname)
85 nil)
86 (readers
87 (try-optimize-slot-reader readers args))
88 (writers
89 (try-optimize-slot-writer writers args)))))
90
91 (defun try-optimize-slot-reader (readers args)
92 (let* ((object (first args))
93 (c-object (c1expr object))
94 (readers (precompute-applicable-methods readers (list c-object))))
95 ;(format t "~%;;; Found ~D really applicable reader" (length readers))
96 (when (= (length readers) 1)
97 (let ((reader (first readers)))
98 (when (typep reader 'clos:standard-reader-method *cmp-env*)
99 (let* ((slotd (clos:accessor-method-slot-definition reader))
100 (index (clos::safe-slot-definition-location slotd)))
101 (when (typep index 'fixnum *cmp-env*)
102 `(clos::safe-instance-ref ,object ,index))))))))
103
104 (defun try-optimize-slot-writer (orig-writers args)
105 (let* ((c-args (mapcar #'c1expr args))
106 (writers (precompute-applicable-methods orig-writers c-args)))
107 ;(format t "~%;;; Found ~D really applicable writer" (length writers))
108 (when (= (length writers) 1)
109 (let ((writer (first writers)))
110 (when (typep writer 'clos:standard-writer-method *cmp-env*)
111 (let* ((slotd (clos:accessor-method-slot-definition writer))
112 (index (clos::safe-slot-definition-location slotd)))
113 (when (typep index 'fixnum *cmp-env*)
114 `(si::instance-set ,(second args) ,index ,(first args)))))))))
115
116 #+(or)
117 (progn .
118 #.(loop for var in '(clos::+standard-generic-function-slots+
119 clos::+standard-method-slots+
120 clos::+standard-class-slots+
121 clos::+class-slots+)
122 for slot-list = (symbol-value var)
123 nconc
124 (loop for i from 0
125 for slot-definition in slot-list
126 for accessor = (cadr (member :accessor slot-definition))
127 when accessor
128 collect `(define-compiler-macro ,accessor (&whole whole obj &environment env)
129 (if (policy-inline-slot-access env)
130 `(clos::safe-instance-ref ,obj ,,i)
131 whole)))))