1 ;;;; the VOPs and macro magic required to call static functions
3 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
14 (define-vop (static-fun-template)
17 (:variant-vars function)
19 (:temporary (:sc unsigned-reg :offset ecx-offset
20 :from (:eval 0) :to (:eval 2)) ecx))
22 (eval-when (#-sb-xc :compile-toplevel :load-toplevel :execute)
24 (defun static-fun-template-name (num-args num-results)
25 (intern (format nil "~:@(~R-arg-~R-result-static-fun~)"
26 num-args num-results)))
28 (defun moves (dst src)
30 (do ((dst dst (cdr dst))
32 ((or (null dst) (null src)))
33 (moves `(move ,(car dst) ,(car src))))
36 (defun static-fun-template-vop (num-args num-results)
37 (unless (and (<= num-args register-arg-count)
38 (<= num-results register-arg-count))
39 (error "either too many args (~W) or too many results (~W); max = ~W"
40 num-args num-results register-arg-count))
41 (let ((num-temps (max num-args num-results))
42 (node (gensym "NODE-")))
43 (collect ((temp-names) (temps) (arg-names) (args) (result-names) (results))
44 (dotimes (i num-results)
45 (let ((result-name (intern (format nil "RESULT-~D" i))))
46 (result-names result-name)
47 (results `(,result-name :scs (any-reg descriptor-reg)))))
48 (dotimes (i num-temps)
49 (let ((temp-name (intern (format nil "TEMP-~D" i))))
50 (temp-names temp-name)
51 (temps `(:temporary (:sc descriptor-reg
52 :offset ,(nth i *register-arg-offsets*)
53 :from ,(if (< i num-args)
56 :to ,(if (< i num-results)
59 ,@(when (< i num-results)
60 `(:target ,(nth i (result-names)))))
63 (let ((arg-name (intern (format nil "ARG-~D" i))))
66 :scs (any-reg descriptor-reg)
67 :target ,(nth i (temp-names))))))
68 `(define-vop (,(static-fun-template-name num-args num-results)
72 (:results ,@(results))
74 (:generator ,(+ 50 num-args num-results)
75 ,@(moves (temp-names) (arg-names))
77 ;; If speed is at least as important as size, duplicate the
78 ;; effect of the ENTER with discrete instructions. Takes
79 ;; 3+4+4=11 bytes as opposed to 1+4=5 bytes.
80 (cond ((policy ,node (>= speed space))
81 (inst sub esp-tn (fixnumize 3))
82 (inst mov (make-ea :dword :base esp-tn
83 :disp (frame-byte-offset
88 (inst lea ebp-tn (make-ea :dword :base esp-tn
89 :disp (frame-byte-offset
94 ;; Dummy for return address.
96 (inst enter (fixnumize 1))))
100 `(inst mov ecx (fixnumize ,num-args)))
102 (note-this-location vop :call-site)
103 ;; Old CMU CL comment:
104 ;; STATIC-FUN-OFFSET gives the offset from the start of
105 ;; the NIL object to the static function FDEFN and has the
106 ;; low tag of 1 added. When the NIL symbol value with its
107 ;; low tag of 3 is added the resulting value points to the
108 ;; raw address slot of the fdefn (at +4).
109 ;; FIXME: Since the fork from CMU CL, we've swapped
110 ;; FUN-POINTER-LOWTAG and INSTANCE-POINTER-LOWTAG, so the
111 ;; text above is no longer right. Mysteriously, things still
112 ;; work. It would be good to explain why. (Is this code no
113 ;; longer executed? Does it not depend on the
114 ;; 1+3=4=fdefn_raw_address_offset relationship above?
115 ;; Is something else going on?)
116 (inst call (make-ea :dword
118 (static-fun-offset function))))
119 ,(collect ((bindings) (links))
120 (do ((temp (temp-names) (cdr temp))
121 (name 'values (gensym))
126 (make-tn-ref ,(car temp) nil)))
128 (links `(setf (tn-ref-across ,prev) ,name))))
131 (default-unknown-values
133 ,(if (zerop num-results) nil 'values)
136 ,@(moves (result-names) (temp-names)))))))
140 (macrolet ((frob (num-args num-res)
141 (static-fun-template-vop (eval num-args) (eval num-res))))
147 (defmacro define-static-fun (name args &key (results '(x)) translate
148 policy cost arg-types result-types)
150 ,(static-fun-template-name (length args)
153 (:note ,(format nil "static-fun ~@(~S~)" name))
155 `((:translate ,translate)))
157 `((:policy ,policy)))
159 `((:generator-cost ,cost)))
161 `((:arg-types ,@arg-types)))
163 `((:result-types ,@result-types)))))