1 ;;;; VOPs and macro magic for calling 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 symbol)
19 (:temporary (:scs (non-descriptor-reg)) temp)
20 (:temporary (:scs (descriptor-reg)) move-temp)
21 (:temporary (:sc descriptor-reg :offset lra-offset) lra)
22 (:temporary (:scs (descriptor-reg)) func)
23 (:temporary (:sc any-reg :offset nargs-offset) nargs)
24 (:temporary (:sc any-reg :offset ocfp-offset) old-fp)
25 (:temporary (:sc control-stack :offset nfp-save-offset) nfp-save))
28 (eval-when (:compile-toplevel :load-toplevel :execute)
30 (defun static-fun-template-name (num-args num-results)
31 (intern (format nil "~:@(~R-arg-~R-result-static-fun~)"
32 num-args num-results)))
34 (defun moves (dst src)
36 (do ((dst dst (cdr dst))
38 ((or (null dst) (null src)))
39 (moves `(move ,(car dst) ,(car src))))
42 (defun static-fun-template-vop (num-args num-results)
43 (unless (and (<= num-args register-arg-count)
44 (<= num-results register-arg-count))
45 (error "either too many args (~W) or too many results (~W); max = ~W"
46 num-args num-results register-arg-count))
47 (let ((num-temps (max num-args num-results)))
48 (collect ((temp-names) (temps) (arg-names) (args) (result-names) (results))
49 (dotimes (i num-results)
50 (let ((result-name (intern (format nil "RESULT-~D" i))))
51 (result-names result-name)
52 (results `(,result-name :scs (any-reg descriptor-reg)))))
53 (dotimes (i num-temps)
54 (let ((temp-name (intern (format nil "TEMP-~D" i))))
55 (temp-names temp-name)
56 (temps `(:temporary (:sc descriptor-reg
57 :offset ,(nth i *register-arg-offsets*)
58 ,@(when (< i num-args)
59 `(:from (:argument ,i)))
60 ,@(when (< i num-results)
62 :target ,(nth i (result-names)))))
65 (let ((arg-name (intern (format nil "ARG-~D" i))))
68 :scs (any-reg descriptor-reg)
69 :target ,(nth i (temp-names))))))
70 `(define-vop (,(static-fun-template-name num-args num-results)
74 (:results ,@(results))
75 (:generator ,(+ 50 num-args num-results)
76 (let ((lra-label (gen-label))
77 (cur-nfp (current-nfp-tn vop)))
78 ,@(moves (temp-names) (arg-names))
79 (inst ld func null-tn (static-fun-offset symbol))
80 (inst li nargs (fixnumize ,num-args))
82 (store-stack-tn nfp-save cur-nfp))
83 (inst move old-fp cfp-tn)
84 (inst move cfp-tn csp-tn)
85 (inst compute-lra-from-code lra code-tn lra-label temp)
86 (note-this-location vop :call-site)
87 (inst j func (- (ash simple-fun-code-offset word-shift)
89 (inst move code-tn func)
90 (emit-return-pc lra-label)
91 ,(collect ((bindings) (links))
92 (do ((temp (temp-names) (cdr temp))
93 (name 'values (gensym))
98 (make-tn-ref ,(car temp) nil)))
100 (links `(setf (tn-ref-across ,prev) ,name))))
103 (default-unknown-values vop
104 ,(if (zerop num-results) nil 'values)
105 ,num-results move-temp temp lra-label)))
107 (load-stack-tn cur-nfp nfp-save))
108 ,@(moves (result-names) (temp-names))))))))
114 ;;; FIXME! This looks like a candidate for a dotimes to
115 ;;; register-arg-count.
116 (macrolet ((frob (num-args num-res)
117 (static-fun-template-vop (eval num-args) (eval num-res))))
125 (defmacro define-static-fun (name args &key (results '(x)) translate
126 policy cost arg-types result-types)
128 ,(static-fun-template-name (length args)
131 (:note ,(format nil "static-fun ~@(~S~)" name))
133 `((:translate ,translate)))
135 `((:policy ,policy)))
137 `((:generator-cost ,cost)))
139 `((:arg-types ,@arg-types)))
141 `((:result-types ,@result-types)))))