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