Debian Codegraph @

src/compile-i.scm

raw
1 ;;;
2 ;;; compile-i.scm - Inliners of builtin procedures
3 ;;;
4 ;;; Copyright (c) 2004-2024 Shiro Kawai <[email protected]>
5 ;;;
6 ;;; Redistribution and use in source and binary forms, with or without
7 ;;; modification, are permitted provided that the following conditions
8 ;;; are met:
9 ;;;
10 ;;; 1. Redistributions of source code must retain the above copyright
11 ;;; notice, this list of conditions and the following disclaimer.
12 ;;;
13 ;;; 2. Redistributions in binary form must reproduce the above copyright
14 ;;; notice, this list of conditions and the following disclaimer in the
15 ;;; documentation and/or other materials provided with the distribution.
16 ;;;
17 ;;; 3. Neither the name of the authors nor the names of its contributors
18 ;;; may be used to endorse or promote products derived from this
19 ;;; software without specific prior written permission.
20 ;;;
21 ;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 ;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 ;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 ;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 ;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 ;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
27 ;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 ;;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 ;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 ;;;
33
34 ;; If the subr has a directly corresponding VM instruction, the
35 ;; inlining direction is embedded within the subr definition in
36 ;; the stub file. The inliners below deal with more complex
37 ;; situations.
38
39 ;; Some operations (e.g. NUMADD2) has specialized instructions when
40 ;; one of the operands has certain properties (e.g. if one of the operand
41 ;; is a small exact integer, NUMADDI can be used). Such choice of
42 ;; instructions are done in Pass 5 $ASM handler, since they may have
43 ;; more information. The inliner can emit a generic instruction and
44 ;; leave the choice of specialized instructions to the later stage.
45
46 ;; Defines builtin inliner for the existing SUBRs.
47 ;; The binding of NAME must be visible from gauche.internal.
48 (define-macro (define-builtin-inliner name proc)
49 (let1 debug-name (string->symbol #"inliner/~name")
50 `(let1 ,debug-name ,proc
51 (set! (%procedure-inliner ,name) ,debug-name)
52 (%mark-binding-inlinable! (find-module 'gauche.internal) ',name))))
53
54 ;; Generate two argument call of assembler insn INSN unconditionally
55 (define (gen-inliner-arg2 insn)
56 (^[src args]
57 (match args
58 [(x y) ($asm src (list insn) (list x y))]
59 [else (undefined)])))
60
61 ;; If the form has two arg and the latter arg is a constant exact integer
62 ;; that fits insn arg, generate ***I type insn. If both args are constant,
63 ;; replace the form with ($const (proc x y)). Otherwise give up.
64 (define (gen-inliner-argi insn proc)
65 (^[src args]
66 (match args
67 [(n cnt)
68 (let ([cnt-val (check-numeric-constant cnt)]
69 [n-val (check-numeric-constant n)])
70 (cond [(and cnt-val n-val) ($const (proc n-val cnt-val))]
71 [(and cnt-val (integer-fits-insn-arg? cnt-val))
72 ($asm src `(,insn ,cnt-val) (list n))]
73 [else (undefined)]))]
74 [else (undefined)])))
75
76 (inline-stub
77 (define-cproc %procedure-inliner (proc::<procedure>)
78 (setter (proc::<procedure> inliner) ::<void>
79 (set! (-> proc inliner) inliner))
80 (return (?: (-> proc inliner) (-> proc inliner) '#f)))
81
82 (define-cproc %mark-binding-inlinable! (mod::<module> name::<symbol>) ::<void>
83 (let* ([g::ScmGloc* (Scm_FindBinding mod name 0)])
84 (unless g
85 (Scm_Error "[internal] %%mark-binding-inlinable!: \
86 no such binding for %S in %S"
87 (SCM_OBJ name) (SCM_OBJ mod)))
88 (Scm_GlocMark g SCM_BINDING_INLINABLE)))
89 )
90
91 ;;--------------------------------------------------------
92 ;; Inlining numeric operators
93 ;;
94
95 ;; (1) VM insturctions are usually binary where the corresponding
96 ;; Scheme operators are variable arity. We analyze the arguments
97 ;; and generate a (possibly nested) $asm clause.
98 ;;
99 ;; (2) We try to fold constant operations. Constant numbers may appear
100 ;; literally, or a result of constant-variable compilation or other
101 ;; constant folding. Except the literal numbers we need to call
102 ;; pass1 first on the argument to see if we can get a constant.
103
104 ;; Returns numeric value if iform is a constant number.
105 (define (check-numeric-constant iform)
106 (and ($const? iform)
107 (number? ($const-value iform))
108 ($const-value iform)))
109
110 (define (ensure-inexact-const numconstval)
111 ($const (inexact numconstval)))
112
113 (define-macro (fold-asm src op insn const x y more)
114 `(let loop ([x ,x] [y ,y] [more ,more])
115 (let ([xval (check-numeric-constant x)]
116 [yval (check-numeric-constant y)])
117 (let1 v (if (and xval yval)
118 (,const (,op xval yval))
119 ($asm ,src `(,,insn) `(,,'x ,,'y)))
120 (if (null? more) v (loop v (car more) (cdr more)))))))
121
122 (define-macro (define-builtin-inliner-+* op unit insn const)
123 `(define-builtin-inliner ,op
124 (^[src args]
125 (match args
126 [() (,const ,unit)]
127 [(x) (if-let1 val (check-numeric-constant x)
128 (,const val)
129 (undefined))] ; let it be handled at runtime
130 [(x y . more) (fold-asm src ,op ,insn ,const x y more)]))))
131
132 (define-builtin-inliner-+* + 0 NUMADD2 $const)
133 (define-builtin-inliner-+* +. 0 NUMIADD2 ensure-inexact-const)
134 (define-builtin-inliner-+* * 1 NUMMUL2 $const)
135 (define-builtin-inliner-+* *. 1 NUMIMUL2 ensure-inexact-const)
136
137 (define-macro (define-builtin-inliner--/ op insn const)
138 `(define-builtin-inliner ,op
139 (^[src args]
140 (match args
141 [() (error "procedure requires at least one argument:" src)]
142 [(x) (if-let1 val (check-numeric-constant x)
143 (,const (,op val))
144 ,(if (eq? op '-)
145 `($asm src `(,NEGATE) (list x))
146 (undefined)))] ; let it be handled at runtime
147 [(x y . more) (fold-asm src ,op ,insn ,const x y more)]))))
148
149 (define-builtin-inliner--/ - NUMSUB2 $const)
150 (define-builtin-inliner--/ -. NUMISUB2 ensure-inexact-const)
151 (define-builtin-inliner--/ /. NUMIDIV2 ensure-inexact-const)
152
153 ;; NB: If we detect exact division-by-zero case, we shouldn't fold
154 ;; the constant and let it be handled at runtime.
155 (define-builtin-inliner /
156 (^[src args]
157 (match args
158 [() (error "procedure requires at least one argument:" src)]
159 [(x) (let1 val (check-numeric-constant x)
160 (if (and val (not (eqv? val 0)))
161 ($const (/ val))
162 (undefined)))]
163 [(x y . more)
164 ;; can't use fold-asm here because of exact zero check
165 (let loop ([x x] [y y] [more more])
166 (let ([xval (check-numeric-constant x)]
167 [yval (check-numeric-constant y)])
168 (let1 v (if (and xval yval (not (and (eqv? yval 0) (exact? xval))))
169 ($const (/ xval yval))
170 ($asm src `(,NUMDIV2) `(,x ,y)))
171 (if (null? more) v (loop v (car more) (cdr more))))))])))
172
173 (define-builtin-inliner = (gen-inliner-arg2 NUMEQ2))
174 (define-builtin-inliner < (gen-inliner-arg2 NUMLT2))
175 (define-builtin-inliner <= (gen-inliner-arg2 NUMLE2))
176 (define-builtin-inliner > (gen-inliner-arg2 NUMGT2))
177 (define-builtin-inliner >= (gen-inliner-arg2 NUMGE2))
178
179 (define-builtin-inliner modulo (gen-inliner-argi NUMMODI modulo))
180 (define-builtin-inliner remainder (gen-inliner-argi NUMREMI remainder))
181 (define-builtin-inliner ash (gen-inliner-argi ASHI ash))
182
183 ;; bitwise and, ior and xor. we treat (op expr const) case specially.
184 (define (builtin-inliner-bitwise opname op opcode unit)
185 ;; Classify the arguments to (integer) constants and non-constants.
186 ;; Integer constants are folded. Returns cons of the folded constant
187 ;; (#f if no constant argument), and the list of iforms for the rest
188 ;; of arguments.
189 (define (classify-args args)
190 (let loop ([args args] [constval #f] [iforms '()])
191 (if (null? args)
192 (cons constval iforms)
193 (let1 val (check-numeric-constant (car args))
194 (if (and val (exact-integer? val))
195 (loop (cdr args) (if constval (op constval val) val) iforms)
196 (loop (cdr args) constval (cons (car args) iforms)))))))
197
198 (^[src args]
199 (match (classify-args args)
200 [(#f) ($const unit)]
201 [(constval) ($const constval)]
202 [(#f x) (undefined)]
203 [(constval x) ($asm src `(,opcode) (list ($const constval) x))]
204 [(#f x y) ($asm src `(,opcode) (list x y))]
205 [_ (undefined)])))
206
207 (define-builtin-inliner logand
208 (builtin-inliner-bitwise 'logand logand LOGAND -1))
209 (define-builtin-inliner logior
210 (builtin-inliner-bitwise 'logior logior LOGIOR 0))
211 (define-builtin-inliner logxor
212 (builtin-inliner-bitwise 'logxor logxor LOGXOR 0))
213
214 ;;--------------------------------------------------------
215 ;; Inlining other operators
216 ;;
217
218 (define-builtin-inliner vector-ref
219 (^[src args]
220 (match args
221 [(vec ind) ($asm src `(,VEC-REF) (list vec ind))]
222 [else (undefined)])))
223
224 (define-builtin-inliner vector-set!
225 (^[src args]
226 (match args
227 [(vec ind val) ($asm src `(,VEC-SET) `(,vec ,ind ,val))]
228 [else (error "wrong number of arguments for vector-set!:" src)])))
229
230 (define-macro (define-builtin-inliner-uvref tag TAG)
231 (let ([%-ref (symbol-append tag 'vector-ref)]
232 [%type (symbol-append 'SCM_UVECTOR_ TAG)])
233 `(define-builtin-inliner ,%-ref
234 (^[src args]
235 (match args
236 [(vec ind) ($asm src `(,UVEC-REF ,,%type) `(,vec ,ind))]
237 [else (undefined)])))))
238
239 (define-builtin-inliner-uvref s8 S8)
240 (define-builtin-inliner-uvref u8 U8)
241 (define-builtin-inliner-uvref s16 S16)
242 (define-builtin-inliner-uvref u16 U16)
243 (define-builtin-inliner-uvref s32 S32)
244 (define-builtin-inliner-uvref u32 U32)
245 (define-builtin-inliner-uvref s64 S64)
246 (define-builtin-inliner-uvref u64 U64)
247 (define-builtin-inliner-uvref f16 F16)
248 (define-builtin-inliner-uvref f32 F32)
249 (define-builtin-inliner-uvref f64 F64)
250
251 (define-builtin-inliner uvector-ref
252 (^[src args]
253 (match args
254 [(vec ind) ($asm src `(,UVEC-REF ,SCM_UVECTOR_GENERIC) `(,vec ,ind))]
255 [else (undefined)])))
256
257 (define-builtin-inliner zero?
258 (^[src args]
259 (match args
260 [(arg) ($asm src `(,NUMEQ2) `(,arg ,($const 0)))]
261 [else (error "wrong number of arguments for zero?:" src)])))
262
263 (define-builtin-inliner acons
264 (^[src args]
265 (match args
266 [(a b c) ($asm src `(,CONS) `(,($asm #f `(,CONS) `(,a ,b)) ,c))]
267 [else (error "wrong number of arguments for acons:" src)])))
268
269 (define-builtin-inliner reverse
270 (^[src args]
271 (match args
272 [(a) ($asm src `(,REVERSE) `(,a))]
273 [else (undefined)])))
274
275 (define-builtin-inliner current-input-port
276 (^[src args]
277 (match args
278 [() ($asm src `(,CURIN) '())]
279 [else (undefined)])))
280
281 (define-builtin-inliner current-output-port
282 (^[src args]
283 (match args
284 [() ($asm src `(,CUROUT) '())]
285 [else (undefined)])))
286
287 (define-builtin-inliner current-error-port
288 (^[src args]
289 (match args
290 [() ($asm src `(,CURERR) '())]
291 [else (undefined)])))
292
293 (define-builtin-inliner with-exception-handler
294 (^[src args]
295 (match args
296 [(handler (? (cut has-tag? <> $LAMBDA) lmda))
297 ($dynenv src
298 (list ($call #f ($gref %exception-handler-mark-key.) '())
299 handler
300 '(push))
301 ($lambda-body lmda))]
302 [else (undefined)])))
303
304 (define-builtin-inliner dynamic-wind
305 (^[src args]
306 (match args
307 [(b t a)
308 (let ([at (make-lvar 'after)]
309 [bt (make-lvar 'before)]
310 [tt (make-lvar 'thunk)]
311 [r (make-lvar 'tmp)])
312 (if (constant-lambda? a)
313 ;; when after thunk is dummy, we don't bother to call it.
314 ;; NB: POP-HANDLERS does not affect value registers, so we don't
315 ;; need to save the result of thunk.
316 ($let src 'let `(,at ,bt ,tt) `(,a ,b ,t)
317 ($seq
318 `(,($call (iform-src b) ($lref bt) '())
319 ,($asm src `(,PUSH-HANDLERS 0) `(,($lref bt) ,($lref at)))
320 ,($call (iform-src t) ($lref tt) '())
321 ,($asm src `(,POP-HANDLERS) '()))))
322 ;; normal path
323 ($let src 'let `(,at ,bt ,tt) `(,a ,b ,t)
324 ($seq
325 `(,($call (iform-src b) ($lref bt) '())
326 ,($asm src `(,PUSH-HANDLERS 0) `(,($lref bt) ,($lref at)))
327 ,($receive #f 0 1 (list r)
328 ($call (iform-src t) ($lref tt) '())
329 ($seq
330 `(,($asm src `(,POP-HANDLERS) '())
331 ,($call (iform-src a) ($lref at) '())
332 ,($asm #f `(,TAIL-APPLY 2)
333 (list ($gref values.) ($lref r))))))
334 )))))]
335 [_ (undefined)])))