8760e33d31baf96508781ecc726eddbb0f9b9740
[sbcl.git] / src / compiler / x86-64 / static-fn.lisp
1 ;;;; the VOPs and macro magic required to call static functions
2
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
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.
11
12 (in-package "SB!VM")
13
14 (define-vop (static-fun-template)
15   (:save-p t)
16   (:policy :safe)
17   (:variant-vars function)
18   (:vop-var vop)
19   (:temporary (:sc unsigned-reg :offset ecx-offset
20                    :from (:eval 0) :to (:eval 2)) ecx))
21
22 (eval-when (#-sb-xc :compile-toplevel :load-toplevel :execute)
23
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)))
27
28 (defun moves (dst src)
29   (collect ((moves))
30     (do ((dst dst (cdr dst))
31          (src src (cdr src)))
32         ((or (null dst) (null src)))
33       (moves `(move ,(car dst) ,(car src))))
34     (moves)))
35
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)
54                                           `(:argument ,i)
55                                           '(:eval 1))
56                                :to ,(if (< i num-results)
57                                         `(:result ,i)
58                                         '(:eval 1))
59                                ,@(when (< i num-results)
60                                    `(:target ,(nth i (result-names)))))
61                               ,temp-name))))
62       (dotimes (i num-args)
63         (let ((arg-name (intern (format nil "ARG-~D" i))))
64           (arg-names arg-name)
65           (args `(,arg-name
66                   :scs (any-reg descriptor-reg)
67                   :target ,(nth i (temp-names))))))
68       `(define-vop (,(static-fun-template-name num-args num-results)
69                     static-fun-template)
70         (:args ,@(args))
71         ,@(temps)
72         (:temporary (:sc unsigned-reg) call-target)
73         (:results ,@(results))
74         (:node-var ,node)
75         (:generator ,(+ 50 num-args num-results)
76          ,@(moves (temp-names) (arg-names))
77
78          ;; If speed is at least as important as size, duplicate the
79          ;; effect of the ENTER with discrete instructions. Takes
80          ;; 3+4+4=11 bytes as opposed to 1+4=5 bytes.
81          (cond ((policy ,node (>= speed space))
82                 (inst sub rsp-tn (* 3 n-word-bytes))
83                 (inst mov (make-ea :qword :base rsp-tn
84                                    :disp (frame-byte-offset
85                                           (+ sp->fp-offset
86                                              -3
87                                              ocfp-save-offset)))
88                       rbp-tn)
89                 (inst lea rbp-tn (make-ea :qword :base rsp-tn
90                                           :disp (frame-byte-offset
91                                                  (+ sp->fp-offset
92                                                     -3
93                                                     ocfp-save-offset)))))
94                (t
95                 ;; Dummy for return address.
96                 (inst push rbp-tn)
97                 (inst enter n-word-bytes)))
98
99          ,(if (zerop num-args)
100               '(inst xor ecx ecx)
101               `(inst mov ecx (fixnumize ,num-args)))
102
103          (note-this-location vop :call-site)
104          ;; Old CMU CL comment:
105          ;;   STATIC-FUN-OFFSET gives the offset from the start of
106          ;;   the NIL object to the static function FDEFN and has the
107          ;;   low tag of 1 added. When the NIL symbol value with its
108          ;;   low tag of 3 is added the resulting value points to the
109          ;;   raw address slot of the fdefn (at +4).
110          ;; FIXME: Since the fork from CMU CL, we've swapped
111          ;; FUN-POINTER-LOWTAG and INSTANCE-POINTER-LOWTAG, so the
112          ;; text above is no longer right. Mysteriously, things still
113          ;; work. It would be good to explain why. (Is this code no
114          ;; longer executed? Does it not depend on the
115          ;; 1+3=4=fdefn_raw_address_offset relationship above?
116          ;; Is something else going on?)
117
118          ;; Need to load the target address into a register, since
119          ;; immediate call arguments are just a 32-bit displacement,
120          ;; which obviously can't work with >4G spaces.
121          (inst mov call-target
122                (make-ea :qword
123                         :disp (+ nil-value (static-fun-offset function))))
124          (inst call call-target)
125          ,(collect ((bindings) (links))
126                    (do ((temp (temp-names) (cdr temp))
127                         (name 'values (gensym))
128                         (prev nil name)
129                         (i 0 (1+ i)))
130                        ((= i num-results))
131                      (bindings `(,name
132                                  (make-tn-ref ,(car temp) nil)))
133                      (when prev
134                        (links `(setf (tn-ref-across ,prev) ,name))))
135                    `(let ,(bindings)
136                      ,@(links)
137                      (default-unknown-values
138                          vop
139                          ,(if (zerop num-results) nil 'values)
140                        ,num-results
141                        ,node)))
142          ,@(moves (result-names) (temp-names)))))))
143
144 ) ; EVAL-WHEN
145
146 (macrolet ((frob (num-args num-res)
147              (static-fun-template-vop (eval num-args) (eval num-res))))
148   (frob 0 1)
149   (frob 1 1)
150   (frob 2 1)
151   (frob 3 1))
152
153 (defmacro define-static-fun (name args &key (results '(x)) translate
154                                   policy cost arg-types result-types)
155   `(define-vop (,name
156                 ,(static-fun-template-name (length args)
157                                            (length results)))
158      (:variant ',name)
159      (:note ,(format nil "static-fun ~@(~S~)" name))
160      ,@(when translate
161          `((:translate ,translate)))
162      ,@(when policy
163          `((:policy ,policy)))
164      ,@(when cost
165          `((:generator-cost ,cost)))
166      ,@(when arg-types
167          `((:arg-types ,@arg-types)))
168      ,@(when result-types
169          `((:result-types ,@result-types)))))