Debian Codegraph @

src/libproc.scm

raw
1 ;;;
2 ;;; libproc.scm - procedure call & return, and other control stuff
3 ;;;
4 ;;; Copyright (c) 2000-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 (select-module gauche.internal)
35 (inline-stub
36 (.include "gauche/priv/configP.h"
37 "gauche/vminsn.h"
38 "gauche/prof.h"
39 "gauche/priv/vmP.h"
40 "gauche/priv/procP.h"))
41
42 ;;;
43 ;;; R5RS
44 ;;;
45
46 (select-module scheme)
47 (define-cproc procedure? (obj) ::<boolean> :fast-flonum :constant
48 SCM_PROCEDUREP)
49
50 (define-cproc apply (proc arg1 :rest args)
51 (inliner TAIL-APPLY)
52 (let* ([head::ScmObj] [tail::ScmObj])
53 (cond [(SCM_NULLP args) (return (Scm_VMApply proc arg1))]
54 [else (set! head (Scm_Cons arg1 SCM_NIL)
55 tail head)
56 (dopairs [cp args]
57 (when (SCM_NULLP (SCM_CDR cp))
58 (when (< (Scm_Length (SCM_CAR cp)) 0)
59 (Scm_Error "improper list not allowed: %S" (SCM_CAR cp)))
60 (SCM_APPEND head tail (SCM_CAR cp))
61 (break))
62 (unless (SCM_PAIRP (SCM_CDR cp))
63 (Scm_Error "improper list not allowed: %S" (SCM_CDR cp)))
64 (SCM_APPEND1 head tail (SCM_CAR cp)))
65 (return (Scm_VMApply proc head))])))
66
67 (define-cproc call-with-current-continuation (proc) Scm_VMCallCC)
68 (define-cproc values (:rest args) :constant (inliner VALUES) Scm_Values)
69 (define-cproc dynamic-wind (pre body post) Scm_VMDynamicWind)
70
71 (define-in-module scheme (call-with-values producer consumer)
72 (receive vals (producer) (apply consumer vals)))
73
74 (define-in-module scheme call/cc call-with-current-continuation)
75
76 ;; SRFI-226
77 (define-cproc continuation? (obj) ::<boolean> Scm_ContinuationP)
78
79 (select-module gauche.internal)
80 ;; for partial continuation. See lib/gauche/partcont.scm
81 (define-cproc %call/pc (proc) (return (Scm_VMCallPC proc)))
82 (define-cproc %reset (proc) (return (Scm_VMReset proc)))
83
84 ;; Continuaton prompts
85 (select-module gauche)
86 (define-cproc make-continuation-prompt-tag (:optional (name #f))
87 Scm_MakePromptTag)
88 (define-cproc default-continuation-prompt-tag ()
89 Scm_DefaultPromptTag)
90 (define-cproc continuation-prompt-tag? (obj) ::<boolean>
91 SCM_PROMPT_TAG_P)
92
93 (define-cproc call-with-continuation-prompt (thunk
94 :optional (prompt-tag #f)
95 (abort-handler #f))
96 Scm_VMCallWithContinuationPrompt)
97 (define-cproc abort-current-continuation (prompt-tag :rest objs)
98 Scm_VMAbortCurrentContinuation)
99
100 ;; Continuation marks
101 (select-module gauche)
102 (define-cproc continuation-marks (cont :optional (prompt-tag #f))
103 (return (Scm_ContinuationMarks cont prompt-tag)))
104 (define-cproc current-continuation-marks (:optional (prompt-tag #f))
105 (return (Scm_CurrentContinuationMarks prompt-tag)))
106
107 (define-cproc continuation-marks? (obj) ::<boolean>
108 (return (SCM_CONTINUATION_MARK_SET_P obj)))
109
110 (define-cfn find-immediate-continuation-mark (vm::ScmVM* key fallback) :static
111 (let* ([p (-> vm denv)])
112 (for [() (SCM_PAIRP p) (set! p (SCM_CDR p))]
113 (cond [(and (-> vm cont) (SCM_EQ p (-> vm cont denv)))
114 (return fallback)]
115 [(SCM_EQ (SCM_CAAR p) key)
116 (return (SCM_CDAR p))]))
117 (return fallback)))
118
119 (define-cproc call-with-immediate-continuation-mark (key proc
120 :optional (fallback #f))
121 (let* ([val (find-immediate-continuation-mark (Scm_VM) key fallback)])
122 (return (Scm_VMApply1 proc val))))
123
124 (define-cproc call-with-current-expression-name (proc)
125 (let* ([name (find-immediate-continuation-mark
126 (Scm_VM)
127 (Scm__GetDenvKey SCM_DENV_KEY_EXPRESSION_NAME)
128 SCM_FALSE)])
129 (return (Scm_VMApply1 proc name))))
130
131 (define-cproc continuation-mark-set? (obj) ::<boolean>
132 (return (SCM_CONTINUATION_MARK_SET_P obj)))
133
134 (define-cproc continuation-mark-set->list (cmset::<continuation-mark-set>? key
135 :optional (prompt-tag #f))
136 (return (Scm_ContinuationMarkSetToList cmset key prompt-tag)))
137
138 (define-cproc continuation-mark-set-first (cmset::<continuation-mark-set>?
139 key :optional (fallback #f)
140 (prompt-tag #f))
141 (let* ([cms::ScmContinuationMarkSet*
142 (?: cmset cmset
143 (SCM_CONTINUATION_MARK_SET (Scm_CurrentContinuationMarks prompt-tag)))]
144 [p (-> cms denv)])
145 (for [() (SCM_PAIRP p) (set! p (SCM_CDR p))]
146 (when (SCM_EQ (SCM_CAAR p) key)
147 (return (SCM_CDAR p))))
148 (return fallback)))
149
150 ;; SRFI-226
151 ;; We use uninterned symbols for unique continuation mark key.
152 ;; Since we don't have specialized procedures for the keys specifically
153 ;; created with make-continuation-mark-key, our continuation-mark-key?
154 ;; returns #t for all objects.
155 (define (make-continuation-mark-key :optional (name 'continuation-mark-key-))
156 (gensym (x->string name)))
157 (define (continuation-mark-key? obj) #t)
158
159 ;;;
160 ;;; Useful gadgets
161 ;;;
162
163 (select-module gauche)
164 ;; 'values' could be used, but sometimes handy to be explicit that
165 ;; we're dealing with a single value.
166 (define-inline (identity x) x)
167
168 ;; SRFI-235
169 ;; using case-lambda allows optimized code in inlined applications
170 (define-inline constantly
171 (case-lambda
172 [(val) (^ _ val)]
173 [vals (^ _ (apply values vals))]))
174
175 ;;;
176 ;;; Generator primitives
177 ;;;
178
179 ;; Generators are just an ordinary procedure, so having these in core
180 ;; comes handy. We used to have these as port-*.
181
182 (select-module gauche.internal)
183 ;; Returns a list of new values generated by each of GENS, having tail
184 ;; as the tail of the returned list. It returns #<eof> when any one of
185 ;; generator returns eof (generation is
186 (define (%generate-values gens . tail)
187 (fold-right (^[g tail]
188 (let1 v (g)
189 (if (or (eof-object? v) (eof-object? tail))
190 (eof-object)
191 (cons v tail))))
192 tail gens))
193
194 (define-in-module gauche (generator-fold fn knil gen . more)
195 (if (null? more)
196 (let loop ([item (gen)]
197 [r knil])
198 (if (eof-object? item)
199 r
200 (let1 r (fn item r)
201 (loop (gen) r))))
202 (let1 gens (cons gen more)
203 (let loop ([knil knil])
204 (let1 items (%generate-values gens knil)
205 (if (eof-object? items)
206 knil
207 (loop (apply fn items))))))))
208
209 ;; This will consume large stack if input file is large.
210 (define-in-module gauche (generator-fold-right fn knil gen . more)
211 (if (null? more)
212 (let loop ([item (gen)])
213 (if (eof-object? item)
214 knil
215 (fn item (loop (gen)))))
216 (let1 gens (cons gen more)
217 (let loop ()
218 (let1 items (%generate-values gens)
219 (if (eof-object? items)
220 knil
221 (apply fn (append items `(,(loop))))))))))
222
223 (define-in-module gauche (generator-for-each fn gen . more)
224 (if (null? more)
225 (let loop ([item (gen)])
226 (unless (eof-object? item)
227 (fn item) (loop (gen))))
228 (let1 gens (cons gen more)
229 (let loop ()
230 (let1 items (%generate-values gens)
231 (unless (eof-object? items)
232 (apply fn items)
233 (loop)))))))
234
235 (define-in-module gauche (generator-map fn gen . more)
236 (if (null? more)
237 (let loop ([item (gen)] [r '()])
238 (if (eof-object? item)
239 (reverse r)
240 (let1 r (cons (fn item) r)
241 (loop (gen) r))))
242 (let1 gens (cons gen more)
243 (let loop ([r '()])
244 (let1 items (%generate-values gens)
245 (if (eof-object? items)
246 (reverse r)
247 (loop (cons (apply fn items) r))))))))
248
249 (define-in-module gauche (generator-find pred gen)
250 (let loop ([item (gen)])
251 (cond [(eof-object? item) #f]
252 [(pred item) item]
253 [else (loop (gen))])))
254
255 ;;;
256 ;;; Profiler
257 ;;;
258
259 (select-module gauche)
260 (define-cproc profiler-start () ::<void> Scm_ProfilerStart)
261 (define-cproc profiler-stop () ::<int> Scm_ProfilerStop)
262 (define-cproc profiler-reset () ::<void> Scm_ProfilerReset)
263
264 (select-module gauche.internal)
265 ;; Autoloaded profiler-get-result will use this.
266 ;; See lib/gauche/vm/profiler.scm
267 (define-cproc profiler-raw-result () Scm_ProfilerRawResult)
268
269 ;;;
270 ;;; Introspection
271 ;;;
272
273 (select-module gauche)
274 ;; Misc. WARNING: API may change
275 (define-cproc subr? (obj) ::<boolean> SCM_SUBRP)
276 (define-cproc closure? (obj) ::<boolean> SCM_CLOSUREP)
277 (define-cproc toplevel-closure? (obj) ::<boolean>
278 (return (and (SCM_CLOSUREP obj) (== (-> (SCM_CLOSURE obj) env) NULL))))
279
280 (define-cproc closure-code (clo::<closure>) (return (SCM_CLOSURE_CODE clo)))
281 (define-cproc method-code (m::<method>)
282 (if (-> m func)
283 ;; code is not available for C-defined method
284 (return SCM_FALSE)
285 (return (SCM_OBJ (-> m data)))))
286
287 (define-cproc procedure-info (proc::<procedure>)
288 (return (SCM_PROCEDURE_INFO proc)))
289
290 ;; procedure-type returns a descriptive type of the given procedure.
291 ;; It is an instance of <^> type constructor (see libtype.scm).
292 ;; procedure-type is computed lazily, and cached to proc->typeHint.
293 ;; If the procedure is precompiled, the type info is serialized to a
294 ;; vector; it is reconstructed to #<^ ...> type when accessed for the
295 ;; first time.
296 (define-cproc procedure-type (proc::<procedure>)
297 (let* ([typehint (-> proc typeHint)])
298 (cond [(SCM_VECTORP typehint)
299 (let* ([reconstruct-proc::(static ScmObj) SCM_UNDEFINED])
300 (SCM_BIND_PROC reconstruct-proc "reconstruct-procedure-type"
301 (Scm_GaucheInternalModule))
302 (let1/cps type (Scm_VMApply2 reconstruct-proc (SCM_OBJ proc) typehint)
303 [proc]
304 (set! (-> (cast ScmProcedure* proc) typeHint) type)
305 (return type)))]
306 [(SCM_FALSEP typehint)
307 (let* ([compute-proc::(static ScmObj) SCM_UNDEFINED])
308 (SCM_BIND_PROC compute-proc "compute-procedure-type"
309 (Scm_GaucheInternalModule))
310 (let1/cps type (Scm_VMApply1 compute-proc (SCM_OBJ proc))
311 [proc]
312 (set! (-> (cast ScmProcedure* proc) typeHint) type)
313 (return type)))]
314 [else (return typehint)])))
315
316 (define-cproc method-leaf? (m::<method>) ::<boolean> SCM_METHOD_LEAF_P)
317
318 ;; NB: This takes a list of classes. But what if we support eqv-specilizer?
319 ;; One idea is to let the caller wrap a concrete instance. We'll see...
320 (define (applicable? proc . arg-types)
321 (define method-applicable?
322 (with-module gauche.object method-applicable-for-classes?))
323 (let1 c (class-of proc)
324 (cond [(eq? c <procedure>)
325 (let1 nargs (length arg-types)
326 (if-let1 infos (case-lambda-decompose proc)
327 (any (^[info] (apply [^(reqargs optarg proc)
328 ((if optarg >= =) nargs reqargs)]
329 info))
330 infos)
331 ((if (slot-ref proc 'optional) >= =)
332 nargs (slot-ref proc 'required))))]
333 [(eq? c <generic>)
334 (any (^m (apply method-applicable? m arg-types)) (~ proc'methods))]
335 [else (apply applicable? object-apply c arg-types)])))
336
337 (select-module gauche.internal)
338 ;; If procedure has a setter and it's locked, return it. Otherwise
339 ;; return #f
340 (define-cproc procedure-locked-setter (proc::<procedure>)
341 (if (and (-> proc locked)
342 (SCM_PROCEDUREP (-> proc setter)))
343 (return (-> proc setter))
344 (return SCM_FALSE)))
345 ;; Returns #t iff obj is a procedure AND constant
346 ;; Note: This also returns #t if obj is generic and sealed. The behavior
347 ;; may change in future, though, so do not rely on it.
348 (define-cproc procedure-constant? (obj) ::<boolean>
349 (return (and (SCM_PROCEDUREP obj)
350 (SCM_PROCEDURE_CONSTANT obj))))
351
352 (define-cproc %procedure-copy (p::<procedure> :optional (tags-alist #f))
353 Scm__CopyProcedure)
354
355 (define-cproc %procedure-tags-alist (p::<procedure>)
356 (return (-> p tagsAlist)))
357
358 (select-module gauche.internal)
359 ;; Make the environment the closure closes into a list and return it.
360 ;; Used from gauche.test.
361 (define-cproc %closure-env->list (clo::<closure>)
362 (let* ([env::ScmEnvFrame* (SCM_CLOSURE_ENV clo)]
363 [h SCM_NIL]
364 [t SCM_NIL])
365 (when (== env NULL)
366 (return SCM_NIL))
367 (dotimes [i (-> env size)]
368 (SCM_APPEND1 h t (ENV_DATA env i)))
369 (return h)))
370
371 (select-module gauche.internal)
372 ;; Tester procedures
373 ;; These are not meant to be used in the actual Scheme code. They're
374 ;; here to test particular C APIs which wouldn't be called from normal
375 ;; Scheme programs.
376 (define-cproc %apply-rec0 (p) Scm_ApplyRec0)
377 (define-cproc %apply-rec1 (p a) Scm_ApplyRec1)
378 (define-cproc %apply-rec2 (p a b) Scm_ApplyRec2)
379 (define-cproc %apply-rec3 (p a b c) Scm_ApplyRec3)
380 (define-cproc %apply-rec4 (p a b c d) Scm_ApplyRec4)
381 (define-cproc %apply-rec (p :rest args) Scm_ApplyRec)
382
383 (select-module gauche.internal)
384 ;; To add autocurrying flag (experimental)
385 (define-cproc %procedure-currying-set! (p::<procedure> f::<boolean>) ::<void>
386 (set! (SCM_PROCEDURE_CURRYING p) f))