| 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 |
;;;; CMPSTRUCT. STRUCTURE 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 |
;;; GET-SLOT-TYPE |
| 15 |
;;; |
| 16 |
;;; Given a structure type and a slot index, infer the type of the output. |
| 17 |
;;; |
| 18 |
(defun get-slot-type (name index) |
| 19 |
;; default is t |
| 20 |
(or (third (nth index (si:get-sysprop name 'si:structure-slot-descriptions))) 'T)) |
| 21 |
|
| 22 |
;;; |
| 23 |
;;; STRUCTURE SLOT READING |
| 24 |
;;; |
| 25 |
;;; By looking at the name of a function we may infer whether it is a |
| 26 |
;;; reader for a structure slot. If this is the case and the policy |
| 27 |
;;; allows us, we will inline the slot access and infer the type of |
| 28 |
;;; the output. |
| 29 |
;;; |
| 30 |
|
| 31 |
(defun maybe-optimize-structure-access (fname args) |
| 32 |
(let* ((slot-description (si:get-sysprop fname 'si::structure-access))) |
| 33 |
(when (and slot-description |
| 34 |
(inline-possible fname) |
| 35 |
(policy-inline-slot-access-p)) |
| 36 |
;(format t "~%;;; Optimizing structure accessor ~A" fname) |
| 37 |
(let (structure-type slot-index) |
| 38 |
(unless (and (consp slot-description) |
| 39 |
(setf structure-type (car slot-description) |
| 40 |
slot-index (cdr slot-description)) |
| 41 |
(typep slot-index 'fixnum *cmp-env*)) |
| 42 |
(cmpwarn "Unable to inline access to structure slot ~A because index is corrupt: ~A" |
| 43 |
fname slot-index) |
| 44 |
(return-from maybe-optimize-structure-access nil)) |
| 45 |
(unless (= (length args) 1) |
| 46 |
(cmpwarn "Too many arguments for structure slot accessor ~A" fname) |
| 47 |
(return-from maybe-optimize-structure-access nil)) |
| 48 |
(setf args (first args)) |
| 49 |
(cond |
| 50 |
((eq structure-type 'list) |
| 51 |
`(elt ,args ,slot-index)) |
| 52 |
((eq structure-type 'vector) |
| 53 |
`(svref ,args ,slot-index)) |
| 54 |
((consp structure-type) |
| 55 |
`(aref (the ,structure-type ,args) ,slot-index)) |
| 56 |
(t |
| 57 |
`(,args ',structure-type ,slot-index))))))) |
| 58 |
|
| 59 |
(define-compiler-macro si:structure-ref (&whole whole object structure-name index |
| 60 |
&environment env) |
| 61 |
(if (and (policy-inline-slot-access env) |
| 62 |
(constantp structure-name env) |
| 63 |
(constantp index env)) |
| 64 |
(let* ((index (ext:constant-form-value index env)) |
| 65 |
(aux (gensym)) |
| 66 |
(form `(ffi:c-inline (,aux ,index) (:object :fixnum) :object |
| 67 |
"(#0)->instance.slots[#1]" |
| 68 |
:one-liner t))) |
| 69 |
(unless (policy-assume-no-errors env) |
| 70 |
(setf form |
| 71 |
(let ((structure-name (ext:constant-form-value structure-name env))) |
| 72 |
`(ext:compiler-typecase ,aux |
| 73 |
(,structure-name ,form) |
| 74 |
(t (ffi:c-inline (,aux ,structure-name ,index) |
| 75 |
(:object :object :fixnum) |
| 76 |
:object |
| 77 |
"ecl_structure_ref(#0,#1,#2)" |
| 78 |
:one-liner t)))))) |
| 79 |
`(let ((,aux ,object)) |
| 80 |
(declare (:read-only ,aux)) |
| 81 |
,form)) |
| 82 |
whole)) |
| 83 |
|
| 84 |
(define-compiler-macro si::structure-set (&whole whole object structure-name index value |
| 85 |
&environment env) |
| 86 |
(if (and (policy-inline-slot-access env) |
| 87 |
(constantp structure-name env) |
| 88 |
(constantp index env)) |
| 89 |
(let* ((index (ext:constant-form-value index env)) |
| 90 |
(aux (gensym)) |
| 91 |
(form `(ffi:c-inline (,aux ,index ,value) (:object :fixnum :object) :object |
| 92 |
"(#0)->instance.slots[#1]=#2" |
| 93 |
:one-liner t))) |
| 94 |
(unless (policy-assume-no-errors env) |
| 95 |
(let ((structure-name (ext:constant-form-value structure-name env))) |
| 96 |
(setf form |
| 97 |
`(ext:compiler-typecase |
| 98 |
,aux |
| 99 |
(,structure-name ,form) |
| 100 |
(t (ffi:c-inline (,aux ',structure-name ,index ,value) |
| 101 |
(:object :object :fixnum :object) |
| 102 |
:object |
| 103 |
"ecl_structure_set(#0,#1,#2,#3)" |
| 104 |
:one-liner t)))))) |
| 105 |
`(let ((,aux ,object)) |
| 106 |
(declare (:read-only ,aux)) |
| 107 |
,form)) |
| 108 |
whole)) |