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 (:sc interior-reg :offset lip-offset) entry-point)
23 (:temporary (:sc any-reg :offset nargs-offset) nargs)
24 (:temporary (:sc any-reg :offset ocfp-offset) ocfp)
25 (:temporary (:sc control-stack :offset nfp-save-offset) nfp-save))
27 (eval-when (:compile-toplevel :load-toplevel :execute)
29 (defun static-fun-template-name (num-args num-results)
30 (intern (format nil "~:@(~R-arg-~R-result-static-fun~)"
31 num-args num-results)))
33 (defun moves (src dst)
35 (do ((dst dst (cdr dst))
37 ((or (null dst) (null src)))
38 (moves `(move ,(car src) ,(car dst))))
41 (defun static-fun-template-vop (num-args num-results)
42 (assert (and (<= num-args register-arg-count)
43 (<= num-results register-arg-count))
44 (num-args num-results)
45 "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 null zero)
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 (arg-names) (temp-names))
79 (inst li (fixnumize ,num-args) nargs)
80 (inst ldl entry-point (static-fun-offset symbol) null-tn)
82 (store-stack-tn nfp-save cur-nfp))
83 (inst move cfp-tn ocfp)
84 (inst compute-lra-from-code lra code-tn lra-label temp)
85 (note-this-location vop :call-site)
86 (inst move csp-tn cfp-tn)
87 (inst jsr zero-tn entry-point)
88 (emit-return-pc lra-label)
89 ,(collect ((bindings) (links))
90 (do ((temp (temp-names) (cdr temp))
91 (name 'values (gensym))
96 (make-tn-ref ,(car temp) nil)))
98 (links `(setf (tn-ref-across ,prev) ,name))))
101 (default-unknown-values vop
102 ,(if (zerop num-results) nil 'values)
103 ,num-results move-temp temp lra-label)))
105 (maybe-load-stack-nfp-tn cur-nfp nfp-save temp))
106 ,@(moves (temp-names) (result-names))))))))
111 (collect ((templates (list 'progn)))
112 (dotimes (i register-arg-count)
113 (templates (static-fun-template-vop i 1)))
116 (defmacro define-static-fun (name args &key (results '(x)) translate
117 policy cost arg-types result-types)
119 ,(static-fun-template-name (length args)
122 (:note ,(format nil "static-fun ~@(~S~)" name))
124 `((:translate ,translate)))
126 `((:policy ,policy)))
128 `((:generator-cost ,cost)))
130 `((:arg-types ,@arg-types)))
132 `((:result-types ,@result-types)))))