Debian Codegraph @

src/compile-1.scm

raw
1 ;;;
2 ;;; compile-1.scm - The compiler: Pass 1
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 ;;===============================================================
35 ;; Pass 1
36 ;;
37 ;; Converts S-expr to IForm. Macros are expanded. Variable references
38 ;; are resolved and converted to either $lref or $gref. The constant
39 ;; variable references (defined by define-constant) are converted to
40 ;; its values at this stage.
41
42 ;; Common entry to handle procedure call
43 ;; proc is IForm. args is [Sexpr].
44 (define-inline (pass1/call program proc args cenv)
45 (cond
46 [(has-tag? proc $LAMBDA) ; immediate lambda
47 (expand-inlined-procedure program proc (imap (cut pass1 <> cenv) args))]
48 [(null? args) ($call program proc '())] ; fast path
49 [(> (length args) MAX_LITERAL_ARG_COUNT)
50 (errorf "Too many arguments in the call of `~,,,,40s...'" program)]
51 [else (let1 cenv (cenv-sans-name cenv)
52 ($call program proc (imap (cut pass1 <> cenv) args)))]))
53
54 ;; Check if the head of the list is a variable, and if so, lookup it.
55 ;; Note that we need to detect the case ((with-module foo bar) arg ...)
56 ;; NB: This isn't a proper fix, for we cannot deal with the situation
57 ;; like nested or aliased with-modules. The Right Thing is to run
58 ;; `pass1 for syntax' on (car PROGRAM) and check the result to see if
59 ;; we need to treat PROGRAM as a special form or an ordinary procedure.
60 ;; It would be a large change, so this is a compromise...
61 (define-inline (pass1/lookup-head head cenv)
62 (or (and (identifier? head)
63 (cenv-lookup cenv head))
64 (and (pair? head)
65 (module-qualified-variable? head cenv)
66 (let1 mod (ensure-module (cadr head) 'with-module #f)
67 (cenv-lookup (cenv-swap-module cenv mod) (caddr head))))))
68
69 ;;--------------------------------------------------------------
70 ;; pass1 :: Sexpr, Cenv -> IForm
71 ;;
72 ;; The Pass 1 entry point.
73 ;; This is one of the most frequently called routine. It is critical to
74 ;; make sure all internal functions are inlined, in case you
75 ;; change something.
76 (define (pass1 program cenv)
77
78 ;; Handle a global call. PROGRAM's car is resolved to an identifier, ID.
79 ;; We know PROGRAM is a call to global procedure, macro, or syntax.
80 (define (pass1/global-call id)
81 (receive (gval type) (global-call-type id cenv)
82 (if gval
83 (case type
84 [(macro) (pass1 (call-macro-expander gval program cenv) cenv)]
85 [(syntax) (call-syntax-handler gval program cenv)]
86 [(inline) (or (pass1/expand-inliner program id gval cenv)
87 (pass1/call program ($gref id) (cdr program) cenv))]
88 [(type-ctor)
89 ;; Type constructor have to be called during compilation,
90 ;; and the resulting node becomes ($const <type-instance>).
91 (type/construct gval
92 (pass1/call program ($gref id) (cdr program) cenv)
93 cenv)])
94 (pass1/call program ($gref id) (cdr program) cenv))))
95
96 ;; main body of pass1
97 (cond
98 [(pair? program) ; (op . args)
99 (unless (list? program)
100 (error "proper list required for function application or macro use:" program))
101 (cond
102 [(pass1/lookup-head (car program) cenv)
103 => (^h (cond
104 [(wrapped-identifier? h) (pass1/global-call h)]
105 [(lvar? h) (pass1/call program ($lref h) (cdr program) cenv)]
106 [(macro? h) ;; local macro
107 (pass1 (call-macro-expander h program cenv) cenv)]
108 [(syntax? h);; locally rebound syntax
109 (call-syntax-handler h program cenv)]
110 [else (error "[internal] unknown resolution of head:" h)]))]
111 [(pass1/detect-constant-setter-call (car program) cenv)
112 => (^[setter]
113 (or (pass1/expand-inliner program `(setter ,(car program))
114 setter cenv)
115 (and-let* ([info (~ setter'info)]
116 [binfo (pair-attribute-get info 'bind-info #f)]
117 [mod (find-module (car binfo))]
118 [name (cadr binfo)])
119 (pass1/call program
120 (pass1 (make-identifier name mod '()) cenv)
121 (cdr program) cenv))
122 (pass1/call program (pass1 (car program) (cenv-sans-name cenv))
123 (cdr program) cenv)))]
124 [else (pass1/call program (pass1 (car program) (cenv-sans-name cenv))
125 (cdr program) cenv)])]
126 [(identifier? program) ; variable reference
127 (let1 r (cenv-lookup cenv program)
128 (cond [(lvar? r) ($lref r)]
129 [(wrapped-identifier? r)
130 (let* ([gbind (global-ref-type r)])
131 (if (and (macro? gbind) ;global macro
132 (identifier-macro? gbind))
133 (pass1 (call-id-macro-expander gbind program cenv) cenv)
134 (or (and-let* ([const (find-const-binding r)]) ($const const))
135 ($gref r))))]
136 [(macro? r) ;; local macro appearing in non-head pos
137 (unless (identifier-macro? r)
138 (error "Non-identifier-macro can't appear in this context:" r))
139 (pass1 (call-id-macro-expander r program cenv) cenv)]
140 [else (error "[internal] cenv-lookup returned weird obj:" r)]))]
141 [else ($const program)]))
142
143 ;; If op is (setter <var>), check if <var> has inlinable binding and
144 ;; its setter is locked; if so, returns the setter.
145 ;; There are a bunch of hoops to go through to satisfy the condition.
146 (define (pass1/detect-constant-setter-call op cenv)
147 (and (pair? op) (pair? (cdr op)) (null? (cddr op))
148 (not (vm-compiler-flag-is-set? SCM_COMPILE_NOINLINE_SETTERS))
149 (global-identifier=? (car op) setter.)
150 (and-let* ([var (cadr op)]
151 [ (identifier? var) ] ;ok, <var> is variable
152 [hd (pass1/lookup-head var cenv)]
153 [ (wrapped-identifier? hd) ]
154 [gloc (id->bound-gloc hd)] ; <var> has inlinable binding
155 [val (gloc-ref gloc)]
156 [ (procedure? val) ])
157 (procedure-locked-setter val)))) ;and has locked setter
158
159 ;; Expand inlinable procedure. Returns Maybe IForm
160 ;; NAME is a variable, used for the error message.
161 ;; PROC is <procedure>.
162 ;; NB: This may return #f if inlining is abandoned.
163 (define (pass1/expand-inliner src name proc cenv)
164 ;; TODO: for inline asm, check validity of opcode.
165 (let ([inliner (%procedure-inliner proc)]
166 [args (cdr src)])
167 (match inliner
168 [#f #f] ;no inliner, fallback case
169 [(? integer?) ;VM insn
170 (let ([nargs (length args)]
171 [opt? (slot-ref proc 'optional)])
172 (unless (argcount-ok? args (slot-ref proc 'required) opt?)
173 (errorf "wrong number of arguments: ~a requires ~a, but got ~a"
174 (if (identifier? name) (identifier->symbol name) name)
175 (slot-ref proc 'required) nargs))
176 ;; We might get away with this limit by transforming inline calls
177 ;; to apply or something. Maybe in future.
178 (when (> nargs MAX_LITERAL_ARG_COUNT)
179 (errorf "Too many arguments in the call of `~,,,,40s...'" src))
180 ($asm src (if opt? `(,inliner ,nargs) `(,inliner))
181 (imap (cut pass1 <> cenv) args)))]
182 [(? vector?) ;inlinable lambda
183 (expand-inlined-procedure src
184 (unpack-iform inliner)
185 (imap (cut pass1 <> cenv) args))]
186 [(? macro?)
187 (let1 expanded (call-macro-expander inliner src cenv)
188 (if (eq? src expanded) ;no expansion
189 #f
190 (pass1 expanded cenv)))]
191 [(? procedure?)
192 ;; Call procedural inliner: Src, [IForm] -> IForm
193 ;; The second arg is IForms of arguments.
194 (let1 iform (inliner src (imap (cut pass1 <> cenv) args))
195 (if (undefined? iform) ;no expansion
196 #f
197 iform))]
198 [_ (errorf "[internal] Invalid inliner attached to ~s: ~s"
199 proc inliner)]
200 )))
201
202 ;; This procedure is set in the inliner slot of procedure created by
203 ;; make-case-lambda. If the call site knows the number of arguments,
204 ;; we can skip the dispatching. Returns IForm if inlined, or #<undef>
205 ;; if not.
206 ;; NB: The expansion after procedure selection is almost dupe of
207 ;; pass1/expand-inliner, but we can't consolidate them because the order
208 ;; of applying pass1 differ: pass1/expand-inliner runs pass1 on its arguments
209 ;; by itself, while pass1/make-case-lambda-inliner already gets pass1-ed
210 ;; arguments. Call for refactoring.
211 (define (pass1/make-case-lambda-inliner clambda-proc)
212 (and-let1 info (%case-lambda-info clambda-proc)
213 (let* ([min-args (get-keyword :min-reqargs info)]
214 [max-index (get-keyword :max-optargs info)]
215 [dispatch (get-keyword :dispatch-vector info)])
216 (^[src arg-iforms]
217 (let1 nargs (length arg-iforms)
218 (or (and-let* ([ (<= min-args nargs) ]
219 [index (- nargs min-args)]
220 [index (if (< index max-index) index (- max-index 1))]
221 [proc (vector-ref dispatch index)]
222 [inliner (%procedure-inliner proc)])
223 (cond
224 [(integer? inliner)
225 (let ([opt? (slot-ref proc 'optional)])
226 (if (and (argcount-ok? args (slot-ref proc 'required) opt?)
227 (<= nargs MAX_LITERAL_ARG_COUNT))
228 ($asm src (if opt? `(,inliner ,nargs) `(,inliner))
229 arg-iforms)))]
230 [(vector? inliner)
231 (expand-inlined-procedure src (unpack-iform inliner)
232 arg-iforms)]
233 [(procedure? inliner)
234 (inliner src arg-iforms)]
235 ;; NB: No compiler macros can appear here.
236 [else #f]))
237 (undefined)))))))
238
239 ;; Returns #t iff exp is the form (with-module module VARIABLE)
240 ;; We need to check the global value of with-module, for it might
241 ;; be redefined. We assume this function is called infrequently,
242 ;; thus we can afford the time.
243 (define (module-qualified-variable? expr cenv)
244 (match expr
245 [((? identifier? wm) mod (? identifier? v))
246 (and-let* ([var (cenv-lookup cenv wm)]
247 [ (identifier? var) ])
248 (global-identifier=? var with-module.))]
249 [_ #f]))
250
251 ;;--------------------------------------------------------------
252 ;; pass1/type-expression - Compile input as a type expression
253 ;;
254
255 (define (pass1/type-expression expr cenv)
256 (or (type/ensure (pass1 expr cenv) cenv)
257 (error "Invalid type expression:" expr)))
258
259 ;;--------------------------------------------------------------
260 ;; pass1/body - Compiling body with internal definitions.
261 ;;
262 ;; For the letrec* semantics, we need to build the internal frame
263 ;; as we go though the body, since internal macro definition need to
264 ;; capture the internal environment.
265 ;;
266 ;; (let ((x 1))
267 ;; (define x 2)
268 ;; (define-syntax foo
269 ;; (syntax-rules ()
270 ;; [(_ v) (define v x)])) ;; must refer to inner x
271 ;; (foo y)
272 ;; y) => 2
273 ;;
274 ;; To avoid unnecessary allocation, we adopt somewhat convoluted strategy
275 ;; that delays frame allocation until needed, and once allocated, we
276 ;; "grow" the frame as new definition appears. This is an exception of
277 ;; the general principle that cenv is immutable.
278
279 ;; pass1/body :: [Sexpr], Cenv -> IForm
280 (define (pass1/body exprs cenv)
281 ;; First, we pair up each expr with dummy source info '(). Some of expr
282 ;; may be an 'include' form and expanded into the content of the file,
283 ;; in which case we keep the source file info in each cdr of the pair.
284 (pass1/body-rec (map list exprs) #f #f cenv))
285
286 ;; If we find internal (syntax) definition, we extend cenv with
287 ;; two frames, one for local macros and one for local variables,
288 ;; so that the internal macros can capture the correct scope of
289 ;; identifiers.
290 ;; For the local variables, we insert dummy binding
291 ;; (<name> :rec <init-expr> . <src>)
292 ;; as the placeholder. Once we find the boundary of definitions and
293 ;; expressions, we re-evaluate <init-expr> and replace the frame entry with
294 ;; (<name> . <lvar>)
295 (define (pass1/body-rec exprs mframe vframe cenv)
296 (define (dupe-check var mframe vframe)
297 (when (or (and (pair? mframe) (assq var mframe))
298 (and (pair? vframe) (assq var vframe)))
299 (error "Duplicate internal definitions of" var)))
300 (define (body-macro-expand mac exprs mframe vframe cenv)
301 (match-let1 ((expr . src) . rest) exprs
302 (pass1/body-rec
303 (acons (call-macro-expander mac expr cenv) src rest)
304 mframe vframe cenv)))
305
306 (match exprs
307 [(((op . args) . src) . rest)
308 (or (and-let* ([ (or (not vframe) (not (assq op vframe))) ]
309 [head (pass1/lookup-head op cenv)])
310 (unless (list? args)
311 (error "proper list required for function application \
312 or macro use:" (caar exprs)))
313 (cond
314 [(lvar? head) (pass1/body-finish exprs mframe vframe cenv)]
315 [(macro? head) ; locally defined macro
316 (body-macro-expand head exprs mframe vframe cenv)]
317 [(syntax? head) ; when (let-syntax ((xif if)) (xif ...)) etc.
318 (pass1/body-finish exprs mframe vframe cenv)]
319 [(and (pair? head) (eq? (car head) :rec))
320 (pass1/body-finish exprs mframe vframe cenv)]
321 [(not (wrapped-identifier? head))
322 (error "[internal] pass1/body" head)]
323 [(or (global-identifier=? head define.)
324 (global-identifier=? head define-inline.)
325 (global-identifier=? head r5rs-define.))
326 (let1 def (match args
327 [((name . formals) . body)
328 `(,name :rec (,lambda. ,formals ,@body) . ,src)]
329 [(var init) `(,var :rec ,init . ,src)]
330 [(var)
331 (if (global-identifier=? head r5rs-define.)
332 (error "define without expression is not allowed in R7RS" (caar exprs))
333 `(,var :rec ,(undefined) . ,src))]
334 [_ (error "malformed internal define:" (caar exprs))])
335 (dupe-check (car def) mframe vframe)
336 (if (not mframe)
337 (let* ([cenv (cenv-extend cenv '() SYNTAX)]
338 [mframe (car (cenv-frames cenv))]
339 [cenv (cenv-extend cenv `(,def) LEXICAL)]
340 [vframe (car (cenv-frames cenv))])
341 (pass1/body-rec rest mframe vframe cenv))
342 (begin
343 (push! (cdr vframe) def)
344 (pass1/body-rec rest mframe vframe cenv))))]
345 [(global-identifier=? head define-syntax.) ; internal syntax definition
346 (match args
347 [(name trans-spec)
348 (dupe-check name mframe vframe)
349 (if (not mframe)
350 (let* ([cenv (cenv-extend cenv `((,name)) SYNTAX)]
351 [mframe (car (cenv-frames cenv))]
352 [cenv (cenv-extend cenv `() LEXICAL)]
353 [vframe (car (cenv-frames cenv))]
354 [trans (pass1/eval-macro-rhs
355 'define-syntax trans-spec
356 (cenv-add-name cenv (variable-name name)))])
357 (assq-set! (cdr mframe) name trans)
358 (pass1/body-rec rest mframe vframe cenv))
359 (begin
360 (push! (cdr mframe) `(,name))
361 (let1 trans (pass1/eval-macro-rhs
362 'define-syntax trans-spec
363 (cenv-add-name cenv (variable-name name)))
364 (assq-set! (cdr mframe) name trans)
365 (pass1/body-rec rest mframe vframe cenv))))]
366 [_ (error "syntax-error: malformed internal define-syntax:"
367 `(,op ,@args))])]
368 [(global-identifier=? head begin.) ;intersperse forms
369 (pass1/body-rec (append (imap (cut cons <> src) args) rest)
370 mframe vframe cenv)]
371 [(global-identifier=? head include.)
372 (let1 sexpr&srcs (pass1/expand-include args cenv #f)
373 (pass1/body-rec (append sexpr&srcs rest) mframe vframe cenv))]
374 [(global-identifier=? head include-ci.)
375 (let1 sexpr&srcs (pass1/expand-include args cenv #t)
376 (pass1/body-rec (append sexpr&srcs rest) mframe vframe cenv))]
377 [(wrapped-identifier? head)
378 (or (and-let* ([gloc (id->bound-gloc head)]
379 [gval (gloc-ref gloc)]
380 [ (macro? gval) ])
381 (body-macro-expand gval exprs mframe vframe cenv))
382 (pass1/body-finish exprs mframe vframe cenv))]
383 [else (error "[internal] pass1/body" head)]))
384 (pass1/body-finish exprs mframe vframe cenv))]
385 [_ (pass1/body-finish exprs mframe vframe cenv)]))
386
387 ;; Finishing internal definitions. If we have internal defs, we wrap
388 ;; the rest by letrec.
389 (define (pass1/body-finish exprs mframe vframe cenv)
390 (if (not mframe)
391 (pass1/body-rest exprs cenv)
392 ;; Replace dummy bindings to the real one
393 (let* ([intdefs. (reverse (cdr vframe))]
394 [vars (map car intdefs.)]
395 [lvars (imap make-lvar+ vars)])
396 (set-cdr! vframe (%map-cons vars lvars))
397 ($let #f 'rec* lvars
398 (imap2 (cut pass1/body-init <> <> cenv) lvars (map cddr intdefs.))
399 (pass1/body-rest exprs cenv)))))
400
401 (define (pass1/body-init lvar init&src newenv)
402 (let1 e (if (null? (cdr init&src))
403 (cenv-add-name newenv (lvar-name lvar))
404 (cenv-add-name/source newenv (lvar-name lvar) (cdr init&src)))
405 (rlet1 iexpr (pass1 (car init&src) e)
406 (lvar-initval-set! lvar iexpr))))
407
408 (define (pass1/body-rest exprs cenv)
409 (match exprs
410 [() ($seq '())]
411 [(expr&src) (pass1/body-1 expr&src cenv)]
412 [_ (let1 stmtenv (cenv-sans-name cenv)
413 ($seq (let loop ([exprs exprs] [r '()])
414 (if (null? (cdr exprs))
415 (reverse (cons (pass1/body-1 (car exprs) cenv) r))
416 (loop (cdr exprs)
417 (cons (pass1/body-1 (car exprs) stmtenv) r))))))]))
418
419 (define (pass1/body-1 expr&src cenv)
420 (let1 src (cdr expr&src)
421 (if (string? src)
422 (pass1 (car expr&src) (cenv-swap-source cenv src))
423 (pass1 (car expr&src) cenv))))
424
425 ;;--------------------------------------------------------------
426 ;; Pass1 utilities
427 ;;
428
429 ;; get symbol or id, and returns identifier.
430 (define (ensure-identifier sym-or-id cenv)
431 (if (symbol? sym-or-id)
432 (make-identifier sym-or-id (cenv-module cenv) (cenv-frames cenv))
433 sym-or-id))
434
435 ;; Does the given argument list satisfy procedure's reqargs/optarg?
436 (define (argcount-ok? args reqargs optarg?)
437 (let1 nargs (length args)
438 (or (and (not optarg?) (= nargs reqargs))
439 (and optarg? (>= nargs reqargs)))))
440
441 ;; signal an error if the form is not on the toplevel
442 (define-inline (check-toplevel form cenv)
443 (unless (cenv-toplevel? cenv)
444 (error "syntax-error: the form can appear only in the toplevel:" form)))
445
446 ;; returns a module specified by THING.
447 (define (ensure-module thing name create?)
448 (let1 mod (cond [(identifier? thing) (find-module (identifier->symbol thing))]
449 [(module? thing) thing]
450 [else
451 (errorf "~a requires a module name or a module, but got: ~s"
452 name thing)])
453 (or mod
454 (if create?
455 (make-module (identifier->symbol thing))
456 (errorf "~a: no such module: ~s" name thing)))))
457
458 ;; IFORM must be a $LAMBDA node. This expands the application of IFORM
459 ;; on IARGS (list of IForm) into a mere $LET node.
460 ;; The nodes within IFORM will be reused in the resulting $LET structure,
461 ;; so be careful not to share substructures of IFORM accidentally.
462 (define (expand-inlined-procedure src iform iargs)
463 (let ([lvars ($lambda-lvars iform)]
464 [args (adjust-arglist ($lambda-reqargs iform) ($lambda-optarg iform)
465 iargs ($lambda-name iform))])
466 (for-each (^[lv a] (lvar-initval-set! lv a)) lvars args)
467 ($let src 'let lvars args ($lambda-body iform))))
468
469 ;; Adjust argument list according to reqargs and optarg count.
470 ;; Used in procedure inlining and local call optimization.
471 (define (adjust-arglist reqargs optarg iargs name)
472 (unless (argcount-ok? iargs reqargs (> optarg 0))
473 (errorf "wrong number of arguments: ~a requires ~a, but got ~a"
474 name reqargs (length iargs)))
475 (if (zero? optarg)
476 iargs
477 (receive (reqs opts) (split-at iargs reqargs)
478 (append! reqs (list (if (null? opts) ($const '()) ($list #f opts)))))))
479
480 ;;----------------------------------------------------------------
481 ;; Pass1 syntaxes
482 ;;
483
484 (define-macro (define-pass1-syntax formals module . body)
485 (let ([mod (ecase module
486 [(:null) 'null]
487 [(:gauche) 'gauche]
488 [(:internal) 'gauche.internal])]
489 ;; a trick to assign comprehensive name to body:
490 [name (string->symbol #"syntax/~(car formals)")])
491 `(let ((,name (^ ,(cdr formals) ,@body))
492 (m (find-module ',mod)))
493 (%insert-syntax-binding m ',(car formals)
494 (make-syntax ',(car formals) m ,name)))))
495
496 ;; Pre-resolved global identifiers.
497 ;; With er-macro-transformer, this is done implicitly within rename procedure.
498 ;; But for the forms expanded by the compiler, we know exactly which identifiers
499 ;; should be reffered, so we directly refer to them.
500 (define (global-id id) (make-identifier id (find-module 'gauche) '()))
501 (define (global-id% id) (make-identifier id (find-module 'gauche.internal) '()))
502
503 (define %expression-name-mark-key. (global-id% '%expression-name-mark-key))
504 (define %exception-handler-mark-key. (global-id% '%exception-handler-mark-key))
505 (define %make-er-transformer. (global-id% '%make-er-transformer))
506 (define %make-er-transformer/toplevel. (global-id% '%make-er-transformer/toplevel))
507 (define %make-id-transformer. (global-id% '%make-id-transformer))
508 (define %with-inline-transformer. (global-id% '%with-inline-transformer))
509
510 (define =>. (global-id '=>))
511 (define apply. (global-id 'apply))
512 (define begin. (global-id 'begin))
513 (define current-module. (global-id 'current-module))
514 (define define-inline. (global-id 'define-inline))
515 (define define-syntax. (global-id 'define-syntax))
516 (define define. (global-id 'define))
517 (define eager. (global-id 'eager))
518 (define else. (global-id 'else))
519 (define error. (global-id 'error))
520 (define getter-with-setter. (global-id 'getter-with-setter))
521 (define include-ci. (global-id 'include-ci))
522 (define include. (global-id 'include))
523 (define lambda. (global-id 'lambda))
524 (define lazy. (global-id 'lazy))
525 (define let-keywords*. (global-id 'let-keywords*))
526 (define let-optionals*. (global-id 'let-optionals*))
527 (define let. (global-id 'let))
528 (define make-case-lambda. (global-id% 'make-case-lambda))
529 (define null?. (global-id 'null?))
530 (define quasiquote. (global-id 'quasiquote))
531 (define r5rs-define. (make-identifier 'define (find-module 'null) '()))
532 (define r5rs-lambda. (make-identifier 'lambda (find-module 'null) '()))
533 (define receive. (global-id 'receive))
534 (define setter. (global-id 'setter))
535 (define unless. (global-id 'unless))
536 (define unquote-splicing. (global-id 'unquote-splicing))
537 (define unquote. (global-id 'unquote))
538 (define values. (global-id 'values))
539 (define with-module. (global-id 'with-module))
540
541 ;; Returns an IForm for (values) - useful for define-pass1-syntax that does
542 ;; compile-time things and returns nothing. The delay trick is to create
543 ;; iform only once. The module to call pass1 doesn't matter, for we directly
544 ;; use identifier for gauche#values.
545 (define $values0 (let1 iform (delay (pass1 `(,values.) (make-bottom-cenv)))
546 (^[] (force iform))))
547
548 ;; Definitions ........................................
549
550 ;; Note on constant binding and inlinable binding:
551 ;; define-constant and define-inline both create a binding that
552 ;; is not supposed to be altered, but they have slightly different
553 ;; semantics. Define-constant binds a global variable to a value that
554 ;; is computable at compile time, and serializable to a precompiled
555 ;; file. When the compiler sees a global variable reference with
556 ;; a constant binding, it replaces the reference to the value itself
557 ;; at pass 1. Define-inline can bind a variable to a value that is
558 ;; calculated at runtime. The compiler does not replace the variable
559 ;; references with values, but it freely rearranges the references within
560 ;; the source code. If an inlinable binding is used at the head position,
561 ;; the compiler looks at its value, and if it is known to be bound to
562 ;; an inlinable procedure, the procedure's body is inlined.
563
564 (define-pass1-syntax (define form cenv) :null
565 (pass1/define form form '() #f (cenv-module cenv) cenv))
566
567 (define-pass1-syntax (define form cenv) :gauche
568 (pass1/define form form '() #t (cenv-module cenv) cenv))
569
570 (define-pass1-syntax (define-constant form cenv) :gauche
571 (pass1/define form form '(const) #t (cenv-module cenv) cenv))
572
573 (define-pass1-syntax (define-in-module form cenv) :gauche
574 (match form
575 [(_ module . rest)
576 (pass1/define `(_ . ,rest) form '() #t
577 (ensure-module module 'define-in-module #f)
578 cenv)]
579 [_ (error "syntax-error: malformed define-in-module:" form)]))
580
581 (define (pass1/define form oform flags extended? module cenv)
582 (check-toplevel oform cenv)
583 (match form
584 [(_ (name . args) body ...)
585 (pass1/define `(define ,name
586 ,(with-original-source
587 `(,(if extended? lambda. r5rs-lambda.) ,args ,@body)
588 oform))
589 oform flags extended? module cenv)]
590
591 [(_ name)
592 (if extended?
593 ;; Gauche's define allows R6RS-style (define <name>).
594 (pass1/define `(define ,name ,(undefined))
595 oform flags #t module cenv)
596 ;; R7RS define doesn't allow it.
597 (error "define without an expression is not allowed in R7RS (it is in R6RS):" oform))]
598 [(_ name expr)
599 (unless (identifier? name) (error "syntax-error:" oform))
600 (let1 cenv (cenv-add-name cenv (variable-name name))
601 ;; Hygiene alert
602 ;; If NAME is an identifier, it is inserted by macro expander; we
603 ;; can't simply place it in $define, since it would insert a toplevel
604 ;; definition into the toplevel of macro-definition environment---
605 ;; we don't want a mere macro call would modify different module.
606 ;; We rename it to uninterned symbol, so, even the binding itself
607 ;; is into the macro-definiting module, it won't be visible from
608 ;; other code except the code generated in the same macro expansion.
609 ;; A trick - we directly modify the identifier, so that other forms
610 ;; referring to the same (eq?) identifier can keep referring it.
611 (let1 id (if (wrapped-identifier? name)
612 (%rename-toplevel-identifier! name)
613 (make-identifier name module '()))
614 ;; Insert dummy binding at compile time, if we don't have one yet.
615 ;; This matters when we compile multiple modules at once, and
616 ;; one need to import from another with qualifiers.
617 (unless (vm-compiler-flag-is-set? SCM_COMPILE_LEGACY_DEFINE)
618 (%insert-binding module (unwrap-syntax name)
619 (%uninitialized) '(fresh)))
620 ($define oform flags id
621 (%wrap-as-named-expression oform (pass1 expr cenv) id))))]
622 [_ (error "syntax-error:" oform)]))
623
624 (define (%rename-toplevel-identifier! identifier)
625 (slot-set! identifier 'name (gensym #"~(identifier->symbol identifier)."))
626 identifier)
627
628 ;; Wrap IFORM with $dynenv to carry ID as the name of the expression.
629 ;; Effectively it is the same as
630 ;; (with-continuation-mark '#:expression-name 'id <body>)
631 ;; During evaluation of IFORM, the name can be retrieved from the
632 ;; continuation mark. Note that the name is meaningful
633 ;; only when an expression is in the tail context, so
634 ;; call-with-immediate-continuation-mark should be used.
635 (define (%wrap-as-named-expression src iform id)
636 ($dynenv src (list ($call src ($gref %expression-name-mark-key.) '())
637 ($const (unwrap-syntax id)))
638 iform))
639
640 ;; Inlinable procedure.
641 ;; Inlinable procedure has both properties of a macro and a procedure.
642 ;; It is a bit tricky since the inliner information has to exist
643 ;; both in compile time and execution time.
644 ;;
645 ;; Processing define-inline involves two actions.
646 ;; (1) Process the lambda node to be inlined. A packed IForm should be
647 ;; attached, and if the lambda node closes environment, some code
648 ;; transformation is required.
649 ;; (2) Bind the resulting node to the global name, and mark the binding
650 ;; 'inlinable'.
651 ;;
652 ;; These two are functionally orthogonal. Especially, not all expressions
653 ;; can yield inlinable lambda node as (1). However, to make procedure
654 ;; inlining work effectively, both of these actions are required; that's
655 ;; why we process them together.
656 ;;
657 ;; Steps:
658 ;; 1. Canonicalize the form to (define-inline NAME EXPR).
659 ;; 2. See if EXPR ultimately returns $LAMBDA node. If so, does the
660 ;; node closes local environment? (pass1/check-inlinable-lambda)
661 ;; 3. If EXPR does not yield a closure, we just create 'inlinable'
662 ;; binding (by pass1/make-inlinable-binding)
663 ;; but do not do anything further.
664 ;; 4. If EXPR directly yields a closure without an environment,
665 ;; process the closure (pass1/mark-closure-inlinable!) and
666 ;; then make inlinable binding.
667 ;; 5. If EXPR creates a local environment, we have to transform
668 ;; the closed variables into global variables. See the comment
669 ;; of subst-lvars above for the details of transformation.
670 ;;
671
672 (define-pass1-syntax (define-inline form cenv) :gauche
673 (check-toplevel form cenv)
674 (match form
675 [(_ (name . args) . body)
676 (pass1/define-inline form name `(,lambda. ,args ,@body) cenv)]
677 [(_ name expr)
678 (unless (identifier? name) (error "syntax-error:" form))
679 (pass1/define-inline form name expr cenv)]
680 [_ (error "syntax-error: malformed define-inline:" form)]))
681
682 (define (pass1/define-inline form name expr cenv)
683 (let1 iform (pass1 expr (cenv-add-name cenv (variable-name name)))
684 (receive (closures closed) (pass1/check-inlinable-lambda iform)
685 (cond
686 [(and (null? closures) (null? closed)) ; too complex to inline
687 (pass1/make-inlinable-binding form name iform cenv)]
688 [(null? closed) ; no closed env
689 (ifor-each (cut pass1/mark-closure-inlinable! <> name cenv) closures)
690 (pass1/make-inlinable-binding form name iform cenv)]
691 [else ; inlinable lambda has closed env.
692 ;; See the comment in subst-lvars above on the transformation.
693 ;; closed :: [(lvar . init-iform)]
694 ;; gvars :: [(identifier . iform)]
695 ;; subs :: [(lvar . iform)] ; iform being $const or $gref
696 (receive (gvars subs)
697 (pass1/define-inline-classify-env name closed cenv)
698 (let1 defs (pass1/define-inline-gen-closed-env gvars cenv)
699 (ifor-each
700 (^[closure]
701 ($lambda-body-set! closure
702 (subst-lvars ($lambda-body closure) subs))
703 (pass1/mark-closure-inlinable! closure name cenv))
704 closures)
705 ($seq `(,@defs
706 ,(pass1/make-inlinable-binding form name iform cenv)))))]
707 ))))
708
709 ;; See if IFORM eventaully generate a closure (or closures). It is the
710 ;; cases when the final expression in IFORM becomes $LAMBDA node or $CLAMBDA
711 ;; node.
712 ;; The closure(s) may close local environment, if the form is something like
713 ;; ($let ... ($lambda ...)).
714 ;; Returns two values:
715 ;; A list of closures: ($lambda-node ...)
716 ;; Alist of closed variables: ((var . init) ...)
717
718 ;; Typical case is ($let ... ($lambda ...)). In such case this
719 ;; procedure effectively strips $let nodes.
720 ;; If IFORM has more complicated structure, we just return (values #f #f)
721 ;; to give up inlining.
722 (define (pass1/check-inlinable-lambda iform)
723 (cond [(has-tag? iform $LAMBDA) (values `(,iform) '())]
724 [(has-tag? iform $CLAMBDA)
725 ;; NB: At pass1, $clambda-closures are all $LAMBDA node.
726 ;; (Beware that we might have $LREF or $GREF in the later passes.)
727 (values ($clambda-closures iform) '())]
728 [(has-tag? iform $LET)
729 (receive (closures closed)
730 (pass1/check-inlinable-lambda ($let-body iform))
731 (if (and (null? closures) (null? closed))
732 (values '() '()) ; giveup
733 (let loop ([lvars (reverse ($let-lvars iform))]
734 [inits (reverse ($let-inits iform))]
735 [closed closed])
736 (if (null? lvars)
737 (values closures closed)
738 (loop (cdr lvars) (cdr inits)
739 (acons (car lvars) (car inits) closed))))))]
740 ;; Special treatment of
741 ;; (define-inline foo (getter-with-setter (lambda ...) (lambda.. )))
742 [(and (has-tag? iform $CALL)
743 (has-tag? ($call-proc iform) $GREF)
744 (global-identifier=? ($gref-id ($call-proc iform))
745 getter-with-setter.)
746 (= (length ($call-args iform)) 2))
747 (receive (closures1 closed1)
748 (pass1/check-inlinable-lambda (car ($call-args iform)))
749 (receive (closures2 closed2)
750 (pass1/check-inlinable-lambda (cadr ($call-args iform)))
751 (values (append closures1 closures2)
752 (append closed1 closed2))))]
753 [else (values '() '())]))
754
755 (define (pass1/define-inline-classify-env name lv&inits cenv)
756 (define gvars '())
757 (define subs '())
758 (let loop ([lv&inits lv&inits])
759 (match lv&inits
760 [() (values (reverse gvars) (reverse subs))]
761 [((and (lv . (? $const?)) p) . lv&inits)
762 (push! subs p) (loop lv&inits)]
763 [((lv . init) . lv&inits)
764 (let1 gvar (make-identifier (gensym #"~|name|$~(lvar-name lv).")
765 (cenv-module cenv) '())
766 (push! subs `(,lv . ,($gref gvar)))
767 (push! gvars `(,gvar . ,(subst-lvars init subs)))
768 (loop lv&inits))])))
769
770 ;; gvars :: [(identifier . iform)]
771 (define (pass1/define-inline-gen-closed-env gvars cenv)
772 (imap (^[gv] ($define #f '(inlinable) (car gv) (cdr gv))) gvars))
773
774 ;; set up $LAMBDA node (closure) to be inlinable. If NAME is given,
775 ;; this also inserts the binding to the current compiling environment
776 ;; so that inlining is effective for the rest of the compilation.
777 (define (pass1/mark-closure-inlinable! closure name cenv)
778 (let* ([module (cenv-module cenv)]
779 ;; Dummy-proc is only a placeholder to record the inliner info
780 ;; to be used during compilation of the current compiler unit.
781 ;; Its body doesn't matter, but we need to make sure every dummy-proc
782 ;; is a different instance. If we make it a constant procedure,
783 ;; Gauche's compiler optimizes it to refer to the singleton instance.
784 [dummy-proc (^ _ name)]
785 [packed (pack-iform closure)])
786 ($lambda-inliner-set! closure packed)
787 (when name
788 ;; record inliner function for compiler. this is used only when
789 ;; the procedure needs to be inlined in the same compiler unit.
790 (%insert-binding module (unwrap-syntax name) dummy-proc
791 '(inlinable dummy))
792 (set! (%procedure-inliner dummy-proc) packed))))
793
794 (define (pass1/make-inlinable-binding form name iform cenv)
795 ;; See the comment in pass1/define about renaming the toplevel identifier.
796 (let1 id (if (wrapped-identifier? name)
797 (%rename-toplevel-identifier! name)
798 (make-identifier name (cenv-module cenv) '()))
799 ($define form '(inlinable) id iform)))
800
801 ;; Toplevel macro definitions
802
803 (define-pass1-syntax (define-macro form cenv) :gauche
804 (check-toplevel form cenv)
805 (match form
806 [(_ (name . formals) body ...)
807 ($ pass1/define-macro form name
808 (with-source-info `(,lambda. ,formals ,@body) form)
809 cenv)]
810 [(_ name expr)
811 (pass1/define-macro form name expr cenv)]
812 [_ (error "syntax-error:" form)]))
813
814 (define (with-source-info obj src)
815 (or (and-let* ([ (pair? obj) ]
816 [ (pair? src) ]
817 [si (debug-source-info src)])
818 (extended-cons (car obj) (cdr obj) `((source-info . ,si))))
819 obj))
820
821 (define (pass1/define-macro src name expr cenv)
822 (unless (identifier? name) (error "syntax-error:" src))
823 ;; TODO: macro autoload
824 (let* ([proc (eval expr (cenv-module cenv))]
825 [source-info (debug-source-info expr)]
826 [trans ($ %make-macro-transformer name
827 (^[form env] (apply proc (cdr form)))
828 `((source . ,expr)
829 ,@(cond-list [source-info `(source-info . ,source-info)])))]
830 ;; See the "Hygiene alert" in pass1/define.
831 [id (if (wrapped-identifier? name)
832 (%rename-toplevel-identifier! name)
833 (make-identifier name (cenv-module cenv) '()))])
834 (%insert-syntax-binding (identifier-module id)
835 (unwrap-syntax name)
836 trans)
837 ($const-undef)))
838
839 (define-pass1-syntax (define-syntax form cenv) :null
840 (check-toplevel form cenv)
841 (match form
842 [(_ name expr) (pass1/define-syntax name expr cenv #f)]
843 [_ (error "syntax-error: malformed define-syntax:" form)]))
844
845 (define-pass1-syntax (define-syntax-parameter form cenv) :gauche ;srfi-139
846 (check-toplevel form cenv)
847 (match form
848 [(_ name expr) (pass1/define-syntax name expr cenv #t)]
849 [_ (error "syntax-error: malformed define-syntax:" form)]))
850
851 (define (pass1/define-syntax name expr cenv parameterizable?)
852 (let* ([cenv (cenv-add-name cenv (variable-name name))]
853 [trans (pass1/eval-macro-rhs 'define-syntax expr cenv)]
854 [trans (if parameterizable?
855 (%make-parameterizable-transformer trans name)
856 trans)]
857 ;; See the "Hygiene alert" in pass1/define.
858 [id (if (wrapped-identifier? name)
859 (%rename-toplevel-identifier! name)
860 (make-identifier name (cenv-module cenv) '()))])
861 (%insert-syntax-binding (identifier-module id)
862 (unwrap-syntax name)
863 trans)
864 ($const-undef)))
865
866 ;; Experimental
867 (define-pass1-syntax (define-hybrid-syntax form cenv) :gauche
868 (pass1/define-hybrid-syntax form cenv))
869 (define-pass1-syntax (define-inline/syntax form cenv) :gauche ;deprecated
870 (pass1/define-hybrid-syntax form cenv))
871
872 (define (pass1/define-hybrid-syntax form cenv)
873 (check-toplevel form cenv)
874 (match form
875 [(_ name expr macro-expr)
876 (let* ([cenv (cenv-add-name cenv (variable-name name))]
877 [xformer (pass1/eval-macro-rhs 'define-hybrid-syntax
878 macro-expr cenv)]
879 [body ($call expr ($gref %with-inline-transformer.)
880 (list (pass1 expr cenv)
881 ($const xformer)))])
882 (pass1/make-inlinable-binding form name body cenv))]
883 [_ (error "syntax-error: define-hybrid-syntax")]))
884
885 ;; Returns either <syntax> or <macro>
886 (define (pass1/eval-macro-rhs who expr cenv)
887 (rlet1 transformer ((make-toplevel-closure (compile expr cenv)))
888 (unless (or (is-a? transformer <syntax>)
889 (is-a? transformer <macro>))
890 (errorf "syntax-error: rhs expression of ~a ~s \
891 doesn't yield a syntactic transformer: ~s"
892 who expr transformer))))
893
894 (inline-stub
895 (define-cproc make-toplevel-closure (code::<compiled-code>)
896 (return (Scm_MakeClosure (SCM_OBJ code) NULL)))
897 )
898
899 ;; Macros ...........................................
900
901 (define-pass1-syntax (er-macro-transformer form cenv) :gauche
902 (match form
903 [(_ xformer) (%pass1/er-macro-maker form xformer cenv #f #f)]
904 [_ (error "syntax-error: malformed er-macro-transformer:" form)]))
905
906 (define-pass1-syntax (eri-macro-transformer form cenv) :gauche
907 (match form
908 [(_ xformer) (%pass1/er-macro-maker form xformer cenv #t #f)]
909 [_ (error "syntax-error: malformed eri-macro-transformer:" form)]))
910
911 (define-pass1-syntax (make-id-transformer form cenv) :gauche
912 (match form
913 [(_ macro-expr)
914 (pass1 `(,%make-id-transformer. ,macro-expr ',(cenv-exp-name cenv)) cenv)]
915 [_ (error "syntax-error: malformed eri-id-macro-transformer:" form)]))
916
917 ;; Build an expression to construct er macro at runtime, and run pass 1
918 ;; on it.
919 ;;
920 ;; We need to capture the current CENV as the macro definition
921 ;; environment. There's a catch, though---if we're AOT compiling
922 ;; a macro, the captured CENV must be serializable to a file,
923 ;; which isn't generally the case.
924 ;; So, if we're compiling toplevel, we call a special API that takes
925 ;; the current module and cenv-exp-name, and reconstruct the cenv
926 ;; at runtime.
927 ;; If cenv has local environment, we don't bother that, for the macro
928 ;; will be fully expanded during AOT compilation. HOWEVER - we can't
929 ;; embed cenv as a vector literal (e.g. `',cenv) since quoting will
930 ;; strip all identifier information in cenv. The right thing would be
931 ;; to make cenv as a record. For now, we take advantage that unquoted
932 ;; vector evaluates to itself, and insert cenv without quoting. This
933 ;; has to change if we prohibit unquoted vector literals.
934 (define (%pass1/er-macro-maker form xformer cenv has-inject? id-macro?)
935 ;; We don't save source---it's not so much useful, and we don't want it to
936 ;; take up space in precompiled code.
937 (define info
938 (or (and-let* ([ (pair? form) ]
939 [si (pair-attribute-get form 'source-info #f)])
940 `((source-info . ,si)))
941 '()))
942
943 (if (cenv-toplevel? cenv)
944 (pass1 `(,%make-er-transformer/toplevel.
945 ,xformer ,(cenv-module cenv) ',(cenv-exp-name cenv)
946 ,@(cond-list
947 [has-inject? @ `(:has-inject? ',has-inject?)]
948 [(pair? info) @ `(:info-alist ',info)]
949 [id-macro? @ `(:identifier-macro? #t)]))
950 cenv)
951 (pass1 `(,%make-er-transformer.
952 ,xformer ,cenv
953 ,@(cond-list
954 [has-inject? @ `(:has-inject? ',has-inject?)]
955 [(pair? info) @ `(:info-alist ',info)]
956 [id-macro? @ `(:identifier-macro? #t)]))
957 cenv)))
958
959 (define-pass1-syntax (%macroexpand form cenv) :gauche
960 (match form
961 [(_ expr) ($const (%internal-macro-expand expr cenv #f))]
962 [_ (error "syntax-error: malformed %macroexpand:" form)]))
963
964 (define-pass1-syntax (%macroexpand-1 form cenv) :gauche
965 (match form
966 [(_ expr) ($const (%internal-macro-expand expr cenv #t))]
967 [_ (error "syntax-error: malformed %macroexpand-1:" form)]))
968
969 (define (%internal-macro-expand expr cenv once?)
970 (define (xpand expr)
971 (match expr
972 [((? identifier? op) . args)
973 (let1 var (cenv-lookup cenv op)
974 (cond [(macro? var) (call-macro-expander var expr cenv)]
975 [(wrapped-identifier? var)
976 (if-let1 gval (and-let* ([gloc (id->bound-gloc var)]
977 [gval (gloc-ref gloc)]
978 [ (macro? gval) ])
979 gval)
980 (call-macro-expander gval expr cenv)
981 expr)]
982 [else expr]))]
983 [((? macro? op) . args) (call-macro-expander op expr cenv)]
984 [_ expr]))
985 (if once?
986 (xpand expr)
987 (let loop ([expr expr])
988 (let1 e2 (xpand expr)
989 (if (eq? e2 expr)
990 expr
991 (loop e2))))))
992
993 (define-pass1-syntax (... form cenv) :null
994 (error "invalid syntax:" form))
995
996 (define-pass1-syntax (let-syntax form cenv) :null
997 (match form
998 [(_ ((name trans-spec) ...) body ...)
999 (let* ([trans (map (^[n x] (pass1/eval-macro-rhs
1000 'let-syntax x
1001 (cenv-add-name cenv (variable-name n))))
1002 name trans-spec)]
1003 [newenv (cenv-extend cenv (%map-cons name trans) SYNTAX)])
1004 (pass1/body body newenv))]
1005 [_ (error "syntax-error: malformed let-syntax:" form)]))
1006
1007 (define-pass1-syntax (letrec-syntax form cenv) :null
1008 (match form
1009 [(_ ((name trans-spec) ...) body ...)
1010 (let* ([newenv (cenv-extend cenv (%map-cons name trans-spec) SYNTAX)]
1011 [trans (map (^[n x] (pass1/eval-macro-rhs
1012 'letrec-syntax x
1013 (cenv-add-name newenv (variable-name n))))
1014 name trans-spec)])
1015 (for-each set-cdr! (cdar (cenv-frames newenv)) trans)
1016 (pass1/body body newenv))]
1017 [_ (error "syntax-error: malformed letrec-syntax:" form)]))
1018
1019 (define-pass1-syntax (syntax-rules form cenv) :null
1020 (match form
1021 [(_ (literal ...) rule ...)
1022 ($const (compile-syntax-rules (cenv-exp-name cenv) form #t literal rule
1023 (cenv-module cenv)
1024 (cenv-frames cenv)))]
1025 [(_ (? variable-or-keyword? elli) (literal ...) rule ...)
1026 ;; NB: We allow keyword for ellipsis, so that something like ::: can be
1027 ;; used.
1028 ($const (compile-syntax-rules (cenv-exp-name cenv) form elli literal rule
1029 (cenv-module cenv)
1030 (cenv-frames cenv)))]
1031 [_ (error "syntax-error: malformed syntax-rules:" form)]))
1032
1033 (define-pass1-syntax (syntax-parameterize form cenv) :gauche
1034 (match form
1035 [(_ ((keys trans-specs) ...) body ...)
1036 (let* ([macros
1037 (map (^[key]
1038 (or (and-let* ([ (identifier? key) ]
1039 [r (cenv-lookup cenv key)])
1040 (cond [(wrapped-identifier? r)
1041 (and-let* ([m (global-ref-type r)]
1042 [ (macro? m) ])
1043 m)]
1044 [(macro? r) r]
1045 [else #f]))
1046 (error "syntax-parameterize: identifer bound to a \
1047 macro expected, but got:" key)))
1048 keys)]
1049 [newmacros (map (^[key spec]
1050 (pass1/eval-macro-rhs 'syntax-parameterize spec
1051 (cenv-add-name cenv key)))
1052 keys trans-specs)]
1053 [saves (map (^[newmacro]
1054 (list (macro-transformer newmacro)
1055 (~ newmacro'flags)))
1056 newmacros)])
1057 (dynamic-wind
1058 (^[] (set! saves (map %swap-macro-transformer! macros saves)))
1059 (^[] (pass1/body body cenv))
1060 (^[] (set! saves (map %swap-macro-transformer! macros saves)))))]
1061 [_ (error "Malformed syntax-parameterize:" form)]))
1062
1063 ;; If family ........................................
1064
1065 (define-pass1-syntax (if form cenv) :null
1066 (match form
1067 [(_ test then else)
1068 ($if form (pass1 test (cenv-sans-name cenv))
1069 (pass1 then cenv) (pass1 else cenv))]
1070 [(_ test then)
1071 ($if form (pass1 test (cenv-sans-name cenv))
1072 (pass1 then cenv) ($const-undef))]
1073 [_ (error "syntax-error: malformed if:" form)]))
1074
1075 (define-pass1-syntax (and form cenv) :null
1076 (define (rec exprs)
1077 (match exprs
1078 [() `#(,$CONST #t)]
1079 [(expr) (pass1 expr cenv)]
1080 [(expr . more)
1081 ($if #f (pass1 expr (cenv-sans-name cenv)) (rec more) ($it))]
1082 [_ (error "syntax-error: malformed and:" form)]))
1083 (rec (cdr form)))
1084
1085 (define-pass1-syntax (or form cenv) :null
1086 (define (rec exprs)
1087 (match exprs
1088 [() ($const-f)]
1089 [(expr) (pass1 expr cenv)]
1090 [(expr . more)
1091 ($if #f (pass1 expr (cenv-sans-name cenv)) ($it) (rec more))]
1092 [_ (error "syntax-error: malformed or:" form)]))
1093 (rec (cdr form)))
1094
1095 (define-pass1-syntax (when form cenv) :gauche
1096 (match form
1097 [(_ test expr1 expr2 ...)
1098 (let1 cenv (cenv-sans-name cenv)
1099 ($if form (pass1 test cenv)
1100 ($seq (imap (cut pass1 <> cenv) (cons expr1 expr2)))
1101 ($const-undef)))]
1102 [_ (error "syntax-error: malformed when:" form)]))
1103
1104 (define-pass1-syntax (unless form cenv) :gauche
1105 (match form
1106 [(_ test expr1 expr2 ...)
1107 (let1 cenv (cenv-sans-name cenv)
1108 ($if form (pass1 test cenv)
1109 ($const-undef)
1110 ($seq (imap (cut pass1 <> cenv) (cons expr1 expr2)))))]
1111 [_ (error "syntax-error: malformed unless:" form)]))
1112
1113 (define-pass1-syntax (else form cenv) :null
1114 (error "invalid syntax:" form))
1115 (define-pass1-syntax (=> form cenv) :null
1116 (error "invalid syntax:" form))
1117
1118 (define-pass1-syntax (cond form cenv) :null
1119 (define (process-clauses cls)
1120 (match cls
1121 [() ($const-undef)]
1122 ;; (else . exprs)
1123 [(([? (cut global-eq? <> else. cenv)] exprs ...) . rest)
1124 (unless (null? rest)
1125 (error "syntax-error: 'else' clause followed by more clauses:" form))
1126 ($seq (imap (cut pass1 <> cenv) exprs))]
1127 ;; (test => proc)
1128 [((test [? (cut global-eq? <> =>. cenv)] proc) . rest)
1129 (let ([test (pass1 test cenv)]
1130 [tmp (make-lvar 'tmp)])
1131 (lvar-initval-set! tmp test)
1132 ($let (car cls) 'let
1133 (list tmp)
1134 (list test)
1135 ($if (car cls)
1136 ($lref tmp)
1137 ($call (car cls)
1138 (pass1 proc (cenv-sans-name cenv))
1139 (list ($lref tmp)))
1140 (process-clauses rest))))]
1141 ;; (generator guard => proc) -- SRFI-61 'general cond clause'
1142 [((generator guard [? (cut global-eq? <> =>. cenv)] receiver) . rest)
1143 (let1 tmp (make-lvar 'tmp)
1144 ($receive (car cls) 0 1 (list tmp)
1145 (pass1 generator cenv)
1146 ($if (car cls)
1147 ($asm #f
1148 `(,APPLY 2)
1149 (list (pass1 guard (cenv-sans-name cenv))
1150 ($lref tmp)))
1151 ($asm #f
1152 `(,APPLY 2)
1153 (list (pass1 receiver (cenv-sans-name cenv))
1154 ($lref tmp)))
1155 (process-clauses rest))))]
1156 [((test) . rest) ; (test)
1157 ($if (car cls) (pass1 test (cenv-sans-name cenv))
1158 ($it)
1159 (process-clauses rest))]
1160 [((test exprs ...) . rest) ; (test . exprs)
1161 ($if (car cls) (pass1 test (cenv-sans-name cenv))
1162 ($seq (imap (cut pass1 <> cenv) exprs))
1163 (process-clauses rest))]
1164 [_ (error "syntax-error: bad clause in cond:" form)]))
1165
1166 (match form
1167 [(_) (error "syntax-error: at least one clause is required for cond:" form)]
1168 [(_ clause ...) (process-clauses clause)]
1169 [else (error "syntax-error: malformed cond:" form)]))
1170
1171 (define-pass1-syntax (case form cenv) :null
1172 (define (process-clauses tmpvar cls)
1173 (match cls
1174 [() ($const-undef)]
1175 [(([? (cut global-eq? <> else. cenv)] exprs ...) . rest)
1176 (unless (null? rest)
1177 (error "syntax-error: 'else' clause followed by more clauses:" form))
1178 (match exprs
1179 ;; (else => proc) -- SRFI-87 case clause
1180 [((? (cut global-eq? <> =>. cenv)) proc)
1181 ($call (car cls)
1182 (pass1 proc (cenv-sans-name cenv))
1183 (list ($lref tmpvar)))]
1184 ;; (else . exprs)
1185 [_ ($seq (imap (cut pass1 <> cenv) exprs))])]
1186 [((elts exprs ...) . rest)
1187 (let ([nelts (length elts)]
1188 [elts (map unwrap-syntax elts)])
1189 ($if (car cls)
1190 (case nelts
1191 [(0) ($const-f)]
1192 [(1) (if (symbol? (car elts))
1193 ($eq? #f ($lref tmpvar) ($const (car elts)))
1194 ($eqv? #f ($lref tmpvar) ($const (car elts))))]
1195 [else ($memv #f ($lref tmpvar) ($const elts))])
1196 (match exprs
1197 ;; (elts => proc) -- SRFI-87 case clause
1198 [((? (cut global-eq? <> =>. cenv)) proc)
1199 ($call (car cls)
1200 (pass1 proc (cenv-sans-name cenv))
1201 (list ($lref tmpvar)))]
1202 ;; (elts . exprs)
1203 [_ ($seq (imap (cut pass1 <> cenv) exprs))])
1204 (process-clauses tmpvar (cdr cls))))]
1205 [_ (error "syntax-error: bad clause in case:" form)]))
1206
1207 (match form
1208 [(_)
1209 (error "syntax-error: at least one clause is required for case:" form)]
1210 [(_ expr clause ...)
1211 (let* ([etree (pass1 expr cenv)]
1212 [tmp (make-lvar 'tmp)])
1213 (lvar-initval-set! tmp etree)
1214 ($let form 'let
1215 (list tmp)
1216 (list etree)
1217 (process-clauses tmp clause)))]
1218 [_ (error "syntax-error: malformed case:" form)]))
1219
1220 (define-pass1-syntax (and-let* form cenv) :gauche
1221 (define (process-binds binds body cenv)
1222 (match binds
1223 [() (pass1/body body cenv)]
1224 [((exp) . more)
1225 (if (and (null? more) (null? body))
1226 (pass1 exp (cenv-sans-name cenv))
1227 ($if form (pass1 exp (cenv-sans-name cenv))
1228 (process-binds more body cenv)
1229 ($it)))]
1230 [([? identifier? var] . more)
1231 (if (and (null? more) (null? body))
1232 (pass1 var (cenv-sans-name cenv))
1233 ($if form (pass1 var (cenv-sans-name cenv))
1234 (process-binds more body cenv)
1235 ($it)))]
1236 [(([? identifier? var] init) . more)
1237 (if (and (null? more) (null? body))
1238 (pass1 init (cenv-add-name cenv var))
1239 (let* ([lvar (make-lvar var)]
1240 [newenv (cenv-extend cenv `((,var . ,lvar)) LEXICAL)]
1241 [itree (pass1 init (cenv-add-name cenv var))])
1242 (lvar-initval-set! lvar itree)
1243 ($let form 'let
1244 (list lvar)
1245 (list itree)
1246 ($if form ($lref lvar)
1247 (process-binds more body newenv)
1248 ($it)))))]
1249 [_ (error "syntax-error: malformed and-let*:" form)]))
1250
1251 (match form
1252 [(_ ()) ($const #t)] ;special base case
1253 [(_ binds . body) (process-binds binds body cenv)]
1254 [_ (error "syntax-error: malformed and-let*:" form)]))
1255
1256 ;; Quote and quasiquote ................................
1257
1258 (define (pass1/quote obj)
1259 ($const ($ unwrap-syntax obj
1260 $ not $ vm-compiler-flag-is-set? SCM_COMPILE_MUTABLE_LITERALS)))
1261
1262 (define-pass1-syntax (quote form cenv) :null
1263 (match form
1264 [(_ obj) (pass1/quote obj)]
1265 [else (error "syntax-error: malformed quote:" form)]))
1266
1267 (define-pass1-syntax (quasiquote form cenv) :null
1268 (match form
1269 [(_ obj) (quasi-expand obj cenv)]
1270 [_ (error "syntax-error: malformed quasiquote:" form)]))
1271
1272 (define-pass1-syntax (unquote form cenv) :null
1273 (error "unquote appeared outside quasiquote:" form))
1274
1275 (define-pass1-syntax (unquote-splicing form cenv) :null
1276 (error "unquote-splicing appeared outside quasiquote:" form))
1277
1278 ;; We need these to be bound so that scheme.base can export them
1279 ;; as specified in R7RS.
1280 (define-pass1-syntax (_ form cenv) :null
1281 ($const-undef))
1282
1283 (define-pass1-syntax (... form cenv) :null
1284 ($const-undef))
1285
1286 ;; quasiquote expander
1287
1288 (define (quasi-expand obj cenv)
1289 ;; We want to avoid unnecessary allocation as much as possible.
1290 ;; Current code generates constants not only the obvious constant
1291 ;; case, e.g. `(a b c), but also folds constant variable references,
1292 ;; e.g. (define-constant x 3) then `(,x) generate a constant list '(3).
1293 ;; This extends as far as the pass-1 constant folding goes, so `(,(+ x 1))
1294 ;; also becomes '(4).
1295 ;; NB: The current code allocates lots of intermediate $const node.
1296 (define (quasiquote? v) (global-eq? v quasiquote. cenv))
1297 (define (unquote? v) (global-eq? v unquote. cenv))
1298 (define (unquote-splicing? v) (global-eq? v unquote-splicing. cenv))
1299 (define (unquote*? v) (or (unquote? v) (unquote-splicing? v)))
1300
1301 ;; In the context where there's no outer list to which we intersperse to.
1302 (define (quasi obj level)
1303 (match obj
1304 [((? quasiquote?) x)
1305 (let1 xx (quasi x (+ level 1))
1306 (if ($const? xx)
1307 ($const (list 'quasiquote ($const-value xx)))
1308 ($list obj (list ($const 'quasiquote) xx))))]
1309 [((? unquote?) x)
1310 (if (zero? level)
1311 (pass1 x cenv)
1312 (let1 xx (quasi x (- level 1))
1313 (if ($const? xx)
1314 ($const (list 'unquote ($const-value xx)))
1315 ($list obj (list ($const 'unquote) xx)))))]
1316 [((? unquote*? op) . xs) ;valid unquote is already handled
1317 (if (zero? level)
1318 (errorf "invalid ~a form in this context: ~s" op obj)
1319 (let1 xx (quasi* xs (- level 1))
1320 (if ($const? xx)
1321 ($const (cons op ($const-value xx)))
1322 ($cons obj ($const op) xx))))]
1323 [(? pair?) (quasi* obj level)]
1324 [(? vector?) (quasi-vector obj level)]
1325 [(? wrapped-identifier?) ($const (unwrap-syntax obj))]
1326 [() ($const-nil)]
1327 [_ ($const obj)]))
1328
1329 ;; In the spliceable context. objs is always a list.
1330 (define (quasi* objs level)
1331 ;; NB: we already excluded toplevel quasiquote and unquote
1332 (match objs
1333 [(((? unquote*? op) . xs) . ys)
1334 (let1 yy (quasi* ys level)
1335 (if (zero? level)
1336 ((if (unquote? op) build build@)
1337 (imap (cut pass1 <> cenv) xs) yy)
1338 (let1 xx (quasi* xs (- level 1))
1339 (if (and ($const? xx) ($const? yy))
1340 ($const (acons op ($const-value xx) ($const-value yy)))
1341 ($cons objs ($cons (car objs) ($const op) xx) yy)))))]
1342 [((? unquote*?) . _) ;`(... . ,xs) `(... . ,@xs)
1343 (quasi objs level)]
1344 [((? vector? x) . ys) (quasi-cons objs quasi-vector x ys level)]
1345 [(x . ys) (quasi-cons objs quasi x ys level)]
1346 [_ (quasi objs level)]))
1347
1348 ;; iforms :: [IForm]
1349 ;; rest :: IForm
1350 (define (build iforms rest)
1351 (match iforms
1352 [() rest]
1353 [(x . xs) (let1 xx (build xs rest)
1354 (if (and ($const? x) ($const? xx))
1355 ($const (cons ($const-value x) ($const-value xx)))
1356 ($cons #f x xx)))]))
1357
1358 (define (build@ iforms rest)
1359 (match iforms
1360 [() rest]
1361 [(x . xs) (let1 xx (build@ xs rest)
1362 (if ($const? xx)
1363 (cond [(null? ($const-value xx)) x]
1364 [($const? x) ($const (append ($const-value x)
1365 ($const-value xx)))]
1366 [else ($append #f x xx)])
1367 ($append #f x xx)))]))
1368
1369 (define (quasi-cons src quasi-car x ys level)
1370 (let ([xx (quasi-car x level)]
1371 [yy (quasi* ys level)])
1372 (if (and ($const? xx) ($const? yy))
1373 ($const (cons ($const-value xx) ($const-value yy)))
1374 ($cons src xx yy))))
1375
1376 (define (quasi-vector obj level)
1377 (if (vector-has-splicing? obj)
1378 ($list->vector obj (quasi* (vector->list obj) level))
1379 (let* ([need-construct? #f]
1380 [elts (map (^[elt] (rlet1 ee (quasi elt level)
1381 (unless ($const? ee)
1382 (set! need-construct? #t))))
1383 (vector->list obj))])
1384 (if need-construct?
1385 ($vector obj elts)
1386 ($const (list->vector (map (^[e] ($const-value e)) elts)))))))
1387
1388 (define (vector-has-splicing? obj)
1389 (let loop ((i 0))
1390 (cond [(= i (vector-length obj)) #f]
1391 [(and (pair? (vector-ref obj i))
1392 (unquote-splicing? (car (vector-ref obj i))))]
1393 [else (loop (+ i 1))])))
1394
1395 (quasi obj 0))
1396
1397
1398 ;; Lambda family (binding constructs) ...................
1399
1400 (define-pass1-syntax (lambda form cenv) :null ;RnRS lambda
1401 (match form
1402 [(_ formals . body)
1403 (receive (reqs rest)
1404 (let loop ((xs formals) (ys '()))
1405 (cond [(null? xs) (values (reverse ys) #f)]
1406 [(identifier? xs) (values (reverse ys) xs)]
1407 [(pair? xs)
1408 (unless (identifier? (car xs))
1409 (error "Invalid formal parameter:" (car xs)))
1410 (loop (cdr xs) (cons (car xs) ys))]
1411 [else (error "Invalid formal parameter:" formals)]))
1412 (pass1/vanilla-lambda (add-arg-info form formals)
1413 (if rest (append reqs (list rest)) reqs)
1414 (length reqs) (if rest 1 0) #f
1415 body cenv))]
1416 [_ (error "syntax-error: malformed lambda:" form)]))
1417
1418 (define-pass1-syntax (lambda form cenv) :gauche ;Extended lambda
1419 (match form
1420 [(_ formals . body)
1421 (receive (args formals types nreqs nopts kargs)
1422 (parse-extended-lambda-args formals)
1423 (let1 type (and types
1424 (construct-procedure-type
1425 (pass1-for-type-annotations types args cenv)
1426 (positive? nopts)
1427 '*))
1428 (if (null? kargs)
1429 (pass1/vanilla-lambda (add-arg-info form formals)
1430 args nreqs nopts type body cenv)
1431 ;; Convert extended lambda into vanilla lambda
1432 (let1 restarg (gensym "rest")
1433 (pass1/vanilla-lambda (add-arg-info form formals)
1434 (append args (list restarg))
1435 nreqs 1 type
1436 (pass1/extended-lambda-body form cenv restarg
1437 kargs body)
1438 cenv)))))]
1439 [_ (error "syntax-error: malformed lambda:" form)]))
1440
1441 ;; Add formals list as 'arg-info to the source form.
1442 ;; They're retrieved by compiled-code-attach-source-info.
1443 (define (add-arg-info form formals)
1444 (rlet1 xform (if (extended-pair? form)
1445 form
1446 (extended-cons (car form) (cdr form)))
1447 (pair-attribute-set! xform 'arg-info formals)))
1448
1449 (define (pass1/vanilla-lambda form formals nreqs nopts type body cenv) ; R7RS lambda
1450 (let* ([lvars (imap make-lvar+ formals)]
1451 [lnode ($lambda form (cenv-exp-name cenv) nreqs nopts lvars #f '())]
1452 [newenv (cenv-extend/proc cenv (%map-cons formals lvars)
1453 LEXICAL lnode)])
1454 ($lambda-type-set! lnode type)
1455 ($lambda-body-set! lnode (pass1/body body newenv))
1456 lnode))
1457
1458 (define-pass1-syntax (receive form cenv) :gauche
1459 (match form
1460 [(_ formals expr body ...)
1461 (receive (args formals types nreqs nopts kargs)
1462 (parse-extended-lambda-args formals)
1463 (unless (null? kargs)
1464 (error "syntax-error: extended lambda list isn't allowed in receive:"
1465 form))
1466 (let* ([lvars (imap make-lvar+ args)]
1467 [newenv (cenv-extend cenv (%map-cons args lvars) LEXICAL)])
1468 ($receive form nreqs nopts lvars (pass1 expr cenv)
1469 (pass1/body body newenv))))]
1470 [_ (error "syntax-error: malformed receive:" form)]))
1471
1472 ;; Returns <args>, <formals>, <types>, <nreqs>, <nopts>, <kargs>
1473 ;; <args> is the list of requried args, plus a restarg if any.
1474 ;; <formals> is a copy of input sans type annotation.
1475 ;; <types> is a list of types for required args. If there's a restarg,
1476 ;; the length of <types> is one shorter than the length of <args>.
1477 ;; Can be #f if there's no type annotation.
1478 ;; <nreqs> is the number of required arguments.
1479 ;; <nopts> is 0 if no optional arg, 1 if not.
1480 ;; <kargs> is like (:optional (x #f) (y #f) :rest k) etc. '() if restarg.
1481 (define (parse-extended-lambda-args formals)
1482 ;; If formals don't have type annotations at all, we skip building
1483 ;; copy of formals and type lists.
1484 (if (and (pair? formals) (member ':: formals))
1485 (let loop ([rest formals] [as '()] [fs '()] [ts '()] [n 0])
1486 (match rest
1487 [() (values (reverse as) (reverse fs) (reverse ts) n 0 '())]
1488 [((? keyword-like?) . _)
1489 (values (reverse as) (reverse fs formals) (reverse ts) n 0 rest)]
1490 [(x ':: t . y)
1491 (loop y (cons x as) (cons x fs) (cons t ts) (+ n 1))]
1492 [(x . y)
1493 (loop y (cons x as) (cons x fs) (cons '<top> ts) (+ n 1))]
1494 [x (values (reverse (cons x as)) (reverse fs x) (reverse ts) n 1 '())]))
1495 (let loop ([rest formals] [as '()] [n 0])
1496 (match rest
1497 [() (values (reverse as) formals #f n 0 '())]
1498 [((? keyword-like?) . _) (values (reverse as) formals #f n 0 rest)]
1499 [(x . y) (loop y (cons x as) (+ n 1))]
1500 [x (values (reverse (cons x as)) formals #f n 1 '())]))))
1501
1502 ;; Run pass1 on each type expression in the argument type annotaions.
1503 ;; NB: Written in a way to avoid closure allocation.
1504 (define (pass1-for-type-annotations types args cenv)
1505 (define (type-1 type arg cenv)
1506 (or (type/ensure (pass1 type cenv) cenv)
1507 (errorf "Invalid type expression ~s for argument ~s" type arg)))
1508 (define (type-n types args cenv)
1509 (if (null? types)
1510 '()
1511 (cons (type-1 (car types) (car args) cenv)
1512 (type-n (cdr types) (cdr args) cenv))))
1513 (type-n types args cenv))
1514
1515 ;; Handles extended lambda list. garg is a gensymed var that receives
1516 ;; restarg.
1517 (define (pass1/extended-lambda-body form cenv garg kargs body)
1518 (define (collect-args xs r)
1519 (match xs
1520 [() (values (reverse r) '())]
1521 [((? keyword-like?) . _) (values (reverse r) xs)]
1522 [(var . rest) (collect-args rest (cons var r))]))
1523 (define (parse-kargs c xs os ks r a)
1524 (match xs
1525 [() (expand-opt os ks r a)]
1526 [(k . xs)
1527 (cond
1528 [(global-keyword-eq? k :optional c)
1529 (unless (null? os) (too-many :optional))
1530 (receive (os xs) (collect-args xs '()) (parse-kargs c xs os ks r a))]
1531 [(global-keyword-eq? k :key c)
1532 (unless (null? ks) (too-many :key))
1533 (receive (ks xs) (collect-args xs '()) (parse-kargs c xs os ks r a))]
1534 [(global-keyword-eq? k :rest c)
1535 (when r (too-many :rest))
1536 (receive (rs xs) (collect-args xs '())
1537 (match rs
1538 [(r) (parse-kargs c xs os ks r a)]
1539 [_ (error ":rest keyword in the extended lambda list must be \
1540 followed by exactly one argument:" kargs)]))]
1541 [(global-keyword-eq? k :allow-other-keys c)
1542 (when a (too-many :allow-other-keys))
1543 (receive (a xs) (collect-args xs '())
1544 (match a
1545 [() (parse-kargs c xs os ks r #t)]
1546 [(av) (parse-kargs c xs os ks r av)]
1547 [_ (error ":allow-other-keys keyword in the extended lambda list \
1548 can be followed by zero or one argument:" kargs)]))]
1549 [else (error "invalid extended lambda list:" kargs)])]))
1550 (define (too-many key)
1551 (errorf "too many ~s keywords in the extended lambda list: ~s" key kargs))
1552 (define (expand-opt os ks r a)
1553 (if (null? os)
1554 (if r
1555 `((,let. ((,r ,garg)) ,@(expand-key ks garg a)))
1556 (expand-key ks garg a))
1557 (let ([binds (map (match-lambda
1558 [[? identifier? o] o]
1559 [(o init) `(,o ,init)]
1560 [_ (error "illegal optional argument spec in " kargs)])
1561 os)]
1562 [rest (or r (gensym))])
1563 `((,let-optionals*. ,garg ,(append binds rest)
1564 ,@(if (and (not r) (null? ks) (not a))
1565 ;; TODO: better error message!
1566 `((,unless. (,null?. ,rest)
1567 (,error. "too many arguments for" ',form))
1568 (,let. () ,@(expand-key ks rest a)))
1569 (expand-key ks rest a)))))))
1570 (define (expand-key ks garg a)
1571 (if (null? ks)
1572 (if a
1573 ;; The case when we have :allow-other-keys without :key.
1574 ;; We don't deal with specific keyword arguments, but expecting
1575 ;; the user provides some. Using let-keywords* checks the
1576 ;; argument list is even.
1577 `((,let-keywords*. ,garg ,(if (boolean? a) (gensym) a) ,@body))
1578 body)
1579 (let1 args (map (match-lambda
1580 [[? identifier? o] o]
1581 [(([? keyword-like? key] o) init)
1582 (let1 k (unwrap-syntax-1 key)
1583 `(,o ,k ,init))]
1584 [(o init) `(,o ,init)]
1585 [_ (error "illegal keyword argument spec in " kargs)])
1586 ks)
1587 `((,let-keywords*. ,garg
1588 ,(if a (append args a) args)
1589 ,@body)))))
1590
1591 (parse-kargs cenv kargs '() '() #f #f))
1592
1593 ;; case-lambda (SRFI-16)
1594 ;; we recognize it here so that we can do aggressive inlining.
1595 (define-pass1-syntax (case-lambda form cenv) :gauche
1596 (match form
1597 [(_ (formals . body)) ; special case
1598 (pass1 `(,lambda. ,formals ,@body) cenv)]
1599 [(_) (error "syntax-error: malformed case-lambda:" form)]
1600 [(_ (formals . body) ...)
1601 (let1 closures (map (^[f b] (pass1 `(,lambda. ,f ,@b) cenv)) formals body)
1602 ($clambda form (cenv-exp-name cenv) closures
1603 (compute-clambda-argcounts closures)))]
1604 [_ (error "syntax-error: malformed case-lambda:" form)]))
1605
1606 ;; This is called when constructing and unpacking CLAMBDA nodes.
1607 (define (compute-clambda-argcounts lambda-nodes)
1608 (map (^l (cons ($lambda-reqargs l) ($lambda-optarg l))) lambda-nodes))
1609
1610 ;; Called during optimization. Returns a $LAMBDA node (or, in later passes,
1611 ;; $LREF or $GREF that points to a closure) that will be used to apply
1612 ;; to the given ARGS. ARGS is used only to find the number of arguments;
1613 ;; the elements doesn't matter. Can return #f if there's no applicable body.
1614 (define (select-clambda-body clambda args)
1615 (let1 nargs (length args)
1616 (let loop ([closures ($clambda-closures clambda)]
1617 [argcounts ($clambda-argcounts clambda)])
1618 (match argcounts
1619 [() #f]
1620 [((nreq . nopt) . argcounts)
1621 (if (or (and (= nopt 0) (= nreq nargs))
1622 (and (> nopt 0) (>= nreq nargs)))
1623 (car closures)
1624 (loop (cdr closures) argcounts))]))))
1625
1626 ;;
1627 ;; LET
1628 ;;
1629 (define-pass1-syntax (let form cenv) :null
1630 (match form
1631 [(_ () body ...)
1632 (pass1/body body cenv)]
1633 [(_ ((var expr) ...) body ...)
1634 (let* ([lvars (imap make-lvar+ var)]
1635 [newenv (cenv-extend cenv (%map-cons var lvars) LEXICAL)])
1636 ($let form 'let lvars
1637 (map (^[init lvar]
1638 (rlet1 iexpr
1639 (pass1 init (cenv-add-name cenv (lvar-name lvar)))
1640 (lvar-initval-set! lvar iexpr)))
1641 expr lvars)
1642 (pass1/body body newenv)))]
1643 [(_ name ((var expr) ...) body ...)
1644 (unless (identifier? name) (error "bad name for named let:" name))
1645 ;; Named let. (let name ((var exp) ...) body ...)
1646 ;;
1647 ;; We don't use the textbook expansion here
1648 ;; ((letrec ((name (lambda (var ...) body ...))) name) exp ...)
1649 ;;
1650 ;; Instead, we use the following expansion, except that we cheat
1651 ;; environment during expanding {exp ...} so that the binding of
1652 ;; name doesn't interfere with exp ....
1653 ;;
1654 ;; (letrec ((name (lambda (var ...) body ...))) (name {exp ...}))
1655 ;;
1656 ;; The reason is that this form can be more easily spotted by
1657 ;; our simple-minded closure optimizer in Pass 2.
1658 (let ([lvar (make-lvar name)]
1659 [args (imap make-lvar+ var)]
1660 [argenv (cenv-sans-name cenv)])
1661 (let* ([env1 (cenv-extend cenv `((,name . ,lvar)) LEXICAL)]
1662 [env2 (cenv-extend/name env1 (%map-cons var args) LEXICAL name)]
1663 [lmda ($lambda form name (length args) 0 args
1664 (pass1/body body env2) '())])
1665 (lvar-initval-set! lvar lmda)
1666 ($let form 'rec
1667 (list lvar)
1668 (list lmda)
1669 ($call #f ($lref lvar)
1670 (imap (cut pass1 <> argenv) expr)))))]
1671 [_ (error "syntax-error: malformed let:" form)]))
1672
1673 (define-pass1-syntax (let* form cenv) :null
1674 (match form
1675 [(_ ((var expr) ...) body ...)
1676 (let loop ([vars var] [inits expr] [cenv cenv])
1677 (if (null? vars)
1678 (pass1/body body cenv)
1679 (let* ([lv (make-lvar (car vars))]
1680 [newenv (cenv-extend cenv `((,(car vars) . ,lv)) LEXICAL)]
1681 [iexpr (pass1 (car inits) (cenv-add-name cenv (car vars)))])
1682 (lvar-initval-set! lv iexpr)
1683 ($let #f 'let (list lv) (list iexpr)
1684 (loop (cdr vars) (cdr inits) newenv)))))]
1685 [_ (error "syntax-error: malformed let*:" form)]))
1686
1687 (define-pass1-syntax (letrec form cenv) :null
1688 (pass1/letrec form cenv "letrec" 'rec))
1689
1690 (define-pass1-syntax (letrec* form cenv) :gauche
1691 (pass1/letrec form cenv "letrec*" 'rec*))
1692
1693 (define (pass1/letrec form cenv name type)
1694 (match form
1695 [(_ () body ...)
1696 (pass1/body body cenv)]
1697 [(_ ((var expr) ...) body ...)
1698 (let* ([lvars (imap make-lvar+ var)]
1699 [newenv (cenv-extend cenv (%map-cons var lvars) LEXICAL)])
1700 ($let form type lvars
1701 (map (^[lv init]
1702 (rlet1 iexpr
1703 (pass1 init (cenv-add-name newenv (lvar-name lv)))
1704 (lvar-initval-set! lv iexpr)))
1705 lvars expr)
1706 (pass1/body body newenv)))]
1707 [else (errorf "syntax-error: malformed ~a: ~s" name form)]))
1708
1709 (define-pass1-syntax (do form cenv) :null
1710 (match form
1711 [(_ ((var init . update) ...) (test expr ...) body ...)
1712 (let* ([tmp (make-lvar 'do-proc)]
1713 [args (imap make-lvar+ var)]
1714 [newenv (cenv-extend/proc cenv (%map-cons var args)
1715 LEXICAL 'do-proc)]
1716 [clo ($lambda
1717 form 'do-body (length var) 0 args
1718 ($if #f
1719 (pass1 test newenv)
1720 (if (null? expr)
1721 ($it)
1722 ($seq (imap (cut pass1 <> newenv) expr)))
1723 ($seq
1724 (list
1725 (pass1/body body newenv)
1726 ($call form
1727 ($lref tmp)
1728 (map (match-lambda*
1729 [(() arg) ($lref arg)]
1730 [((expr) _) (pass1 expr newenv)]
1731 [_ (error "bad update expr in do:" form)])
1732 update args)))))
1733 #f)])
1734 (lvar-initval-set! tmp clo)
1735 ($let form 'rec
1736 (list tmp)
1737 (list clo)
1738 ($call form
1739 ($lref tmp)
1740 (map (cute pass1 <> (cenv-sans-name cenv)) init))))]
1741 [else (error "syntax-error: malformed do:" form)]))
1742
1743 ;; Set! ......................................................
1744
1745 (define-pass1-syntax (set! form cenv) :null
1746 (match form
1747 [(_ (op . args) expr)
1748 ;; SRFI-17. We recurse to pass1 on expanded form, for (setter op) might
1749 ;; have a chance of optimization.
1750 (pass1 (with-original-source `((,setter. ,op) ,@args ,expr) form) cenv)]
1751 [(_ name expr)
1752 (unless (identifier? name)
1753 (error "syntax-error: malformed set!:" form))
1754 (let1 var (cenv-lookup cenv name)
1755 (or (and-let* ([ (wrapped-identifier? var) ]
1756 [gbind (global-ref-type var)]
1757 [ (macro? gbind) ]
1758 [(identifier-macro? gbind) ])
1759 (pass1 (call-id-macro-expander gbind form cenv) cenv))
1760 (and (macro? var) ;local id macro
1761 (if (identifier-macro? var)
1762 (pass1 (call-id-macro-expander var form cenv) cenv)
1763 (error "Local macro can't be used here:" form)))
1764 (let1 val (pass1 expr cenv)
1765 (if (lvar? var)
1766 ($lset var val)
1767 ($gset (ensure-identifier var cenv) val)))))]
1768 [_ (error "syntax-error: malformed set!:" form)]))
1769
1770 ;; Begin .....................................................
1771
1772 (define-pass1-syntax (begin form cenv) :null
1773 ($seq (imap (cut pass1 <> cenv) (cdr form))))
1774
1775 ;; Lazy & Delay ..............................................
1776
1777 (define-pass1-syntax (lazy form cenv) :gauche
1778 (match form
1779 [(_ expr) ($asm form `(,PROMISE)
1780 (list (pass1 `(,lambda. () ,expr) cenv)))]
1781 [_ (error "syntax-error: malformed lazy:" form)]))
1782
1783 ;; (delay expr) == (lazy (receive vals expr (apply eager vals)))
1784 (define-pass1-syntax (delay form cenv) :null
1785 (match form
1786 [(_ expr) (let1 vals (gensym "vals")
1787 (pass1 `(,lazy. (,receive. ,vals ,expr
1788 (,apply. ,eager. ,vals)))
1789 cenv))]
1790 [_ (error "syntax-error: malformed delay:" form)]))
1791
1792 ;; Control flow ..............................................
1793
1794 (define-pass1-syntax (with-continuation-mark form cenv) :gauche
1795 (match form
1796 [(_ key val expr)
1797 ($dynenv form `(,(pass1 key cenv)
1798 ,(pass1 val cenv))
1799 (pass1 expr cenv))]
1800 [_ (error "syntax-error: malformed with-continuation-mark:" form)]))
1801
1802 ;; Module related ............................................
1803
1804 (define-pass1-syntax (define-module form cenv) :gauche
1805 (check-toplevel form cenv)
1806 (match form
1807 [(_ name body ...)
1808 (let* ([mod (ensure-module name 'define-module #t)]
1809 [newenv (make-bottom-cenv mod)])
1810 ($seq (imap (cut pass1 <> newenv) body)))]
1811 [_ (error "syntax-error: malformed define-module:" form)]))
1812
1813 (define-pass1-syntax (with-module form cenv) :gauche
1814 (match form
1815 [(_ name body ...)
1816 (let* ([mod (ensure-module name 'with-module #f)]
1817 [newenv (cenv-swap-module cenv mod)])
1818 ($seq (imap (cut pass1 <> newenv) body)))]
1819 [_ (error "syntax-error: malformed with-module:" form)]))
1820
1821 (define-pass1-syntax (select-module form cenv) :gauche
1822 (check-toplevel form cenv)
1823 (match form
1824 [(_ module)
1825 ;; This is the only construct that changes VM's current module.
1826 ;; We also modifies CENV's module, so that select-module has an
1827 ;; effect in the middle of sequence of expressions like
1828 ;; (begin ... (select-module foo) ...)
1829 ;; It is yet debatable that how select-module should interact with EVAL.
1830 (let1 m (ensure-module module 'select-module #f)
1831 (vm-set-current-module m)
1832 (cenv-module-set! cenv m)
1833 ($values0))]
1834 [else (error "syntax-error: malformed select-module:" form)]))
1835
1836 (define-pass1-syntax (current-module form cenv) :gauche
1837 (unless (null? (cdr form))
1838 (error "syntax-error: malformed current-module:" form))
1839 ($const (cenv-module cenv)))
1840
1841 (define-pass1-syntax (export form cenv) :gauche
1842 (%export-symbols (cenv-module cenv) (cdr form))
1843 ($values0))
1844
1845 (define-pass1-syntax (export-all form cenv) :gauche
1846 (unless (null? (cdr form))
1847 (error "syntax-error: malformed export-all:" form))
1848 (%export-all (cenv-module cenv))
1849 ($values0))
1850
1851 (define-pass1-syntax (import form cenv) :gauche
1852 (define (ensure m) (or (find-module m) (error "unknown module" m)))
1853 (define (symbol-but-not-keyword? x)
1854 (and (symbol? x) (not (keyword? x))))
1855 (dolist [f (cdr form)]
1856 (match f
1857 [((? symbol-but-not-keyword? a) (? symbol-but-not-keyword? b) . r)
1858 ;;likely to be an r7rs-style import
1859 (error "This import form looks like R7RS `import', as opposed to \
1860 Gauche `import'. If you're in REPL, type (use scheme.base) \
1861 then (select-module r7rs.user) to enter the R7RS namespace.")]
1862 [(m . r) (process-import (cenv-module cenv) (ensure m) r)]
1863 [m (process-import (cenv-module cenv) (ensure m) '())]))
1864 ($values0))
1865
1866 (define (process-import current imported args)
1867 (let loop ([imported imported]
1868 [args args]
1869 [prefix #f])
1870 (match args
1871 [() (%import-module current imported prefix)]
1872 [(':prefix p . rest)
1873 (loop imported rest (if prefix (string->symbol #"~|p|~prefix") p))]
1874 [(':only (ss ...) . rest)
1875 (let1 m (%make-wrapper-module imported prefix)
1876 (process-import:mapsym
1877 :only (unwrap-syntax ss) #f prefix
1878 (^[sym orig-sym] (unless (%alias-binding m orig-sym imported orig-sym)
1879 (errorf "during processing :only clause: \
1880 binding of ~a isn't exported from ~a"
1881 orig-sym imported))))
1882 (%extend-module m '())
1883 (loop m rest #f))]
1884 [(':except (ss ...) . rest)
1885 (let1 m (%make-wrapper-module imported prefix)
1886 (process-import:mapsym
1887 :except (unwrap-syntax ss) #f prefix
1888 (^[sym orig-sym] (%hide-binding m orig-sym)))
1889 (loop m rest #f))]
1890 [(':rename ((ss ds) ...) . rest)
1891 (let* ([ss (unwrap-syntax ss)]
1892 [ds (unwrap-syntax ds)]
1893 [m0 (if prefix (%make-wrapper-module imported prefix) imported)]
1894 [m (%make-wrapper-module imported #f)])
1895 (process-import:mapsym
1896 :rename ds ss prefix
1897 (^[sym orig-sym] (unless (%alias-binding m sym imported orig-sym)
1898 (errorf "during processing :rename clause: \
1899 binding of ~a isn't exported from ~a"
1900 orig-sym imported))))
1901 (dolist [s ss] (unless (find-binding m s #t) (%hide-binding m s)))
1902 (%extend-module m (list m0))
1903 (loop m rest #f))]
1904 [(other . rest) (error "invalid import spec:" args)])))
1905
1906 ;; Common work to process new bindings in a trampoline module.
1907 ;; Calls PROCESS with each symbols in SYMS and OLD-SYMS, but
1908 ;; symbols in OLD-SYMS are prefix-stripped. OLD-SYMS can be #f
1909 ;; then we assume it is the same as SYMS.
1910 (define (process-import:mapsym who syms old-syms prefix process)
1911 (define (check s)
1912 (unless (symbol? s)
1913 (errorf "~a option of import must take list of symbols, but got: ~s"
1914 who s)))
1915 (for-each (^[sym osym]
1916 (check sym) (check osym)
1917 (process sym (process-import:strip-prefix who osym prefix)))
1918 syms (or old-syms syms)))
1919
1920 (define (process-import:strip-prefix who sym prefix)
1921 (if prefix
1922 (rlet1 sans (symbol-sans-prefix sym prefix)
1923 (unless sans (errorf "~a specifies nonexistent symbol: ~a" who sym)))
1924 sym))
1925
1926 (define-pass1-syntax (extend form cenv) :gauche
1927 (%extend-module (cenv-module cenv)
1928 (imap (^[m] (or (find-module m)
1929 (begin
1930 (%require (module-name->path m))
1931 (find-module m))
1932 (error "undefined module" m)))
1933 (cdr form)))
1934 ($values0))
1935
1936 (define-pass1-syntax (require form cenv) :gauche
1937 (match form
1938 [(_ feature) (%require feature) ($values0)]
1939 [_ (error "syntax-error: malformed require:" form)]))
1940
1941 ;; Include .............................................
1942
1943 (define-pass1-syntax (include form cenv) :gauche
1944 ($seq (map (^p (pass1 (car p) (cenv-swap-source cenv (cdr p))))
1945 (pass1/expand-include (cdr form) cenv #f))))
1946
1947 (define-pass1-syntax (include-ci form cenv) :gauche
1948 ($seq (map (^p (pass1 (car p) (cenv-swap-source cenv (cdr p))))
1949 (pass1/expand-include (cdr form) cenv #t))))
1950
1951 ;; Returns ((Sexpr . Filename) ...)
1952 (define (pass1/expand-include args cenv case-fold?)
1953 (define (do-include filename)
1954 (unless (string? filename)
1955 (error "include requires literal string, but got:" filename))
1956 (let1 iport (pass1/open-include-file filename (cenv-source-path cenv))
1957 (set! (port-case-fold iport) case-fold?)
1958 (pass1/report-include iport #t)
1959 (unwind-protect
1960 ;; This could be written simpler using port->sexp-list, but it would
1961 ;; trigger autoload and reenters to the compiler.
1962 (let loop ([r (read iport)] [forms '()])
1963 (if (eof-object? r)
1964 `((,begin. ,@(reverse forms)) . ,(port-name iport))
1965 (loop (read iport) (cons r forms))))
1966 (pass1/report-include iport #f)
1967 (close-input-port iport))))
1968 (map do-include args))
1969
1970 ;; If filename is relative, we try to resolve it with the source file.
1971 ;; whenever possible.
1972 (define (pass1/open-include-file path includer-path)
1973 (let1 search-paths (if includer-path
1974 (cons (sys-dirname includer-path) *load-path*)
1975 *load-path*)
1976 ;; find-load-file returns either (<found-path> <rest-of-search-paths>)
1977 ;; or (<pseudo-path> <rest-of-search-paths> <thunk-to-open-content>)
1978 ;; see libeval.scm for the details.
1979 (match (find-load-file path search-paths
1980 (cons "" *load-suffixes*)
1981 :allow-archive #t
1982 :relative-dot-path #t)
1983 [(pseudo-path rest open-content) ;archive hook is in effect
1984 (open-content pseudo-path)]
1985 [(found-path rest) (open-input-file found-path :encoding #t)]
1986 [_ (error "Can't find the file to include: " path)])))
1987
1988 ;; Report including.
1989 (define (pass1/report-include iport open?)
1990 (when (vm-compiler-flag-is-set? SCM_COMPILE_INCLUDE_VERBOSE)
1991 (format (current-error-port) ";;~a including ~s\n"
1992 (if open? "Begin" "End") (port-name iport))))
1993
1994 ;; Black magic ........................................
1995
1996 (define-pass1-syntax (eval-when form cenv) :gauche
1997 (match form
1998 [(_ (w ...) expr ...)
1999 ;; check
2000 (let ([wlist
2001 (let loop ((w w) (r '()))
2002 (cond [(null? w) r]
2003 [(memq (car w) '(:compile-toplevel :load-toplevel :execute))
2004 (if (memq (car w) r)
2005 (loop (cdr w) r)
2006 (loop (cdr w) (cons (car w) r)))]
2007 [else
2008 (error "eval-when: situation must be a list of \
2009 :compile-toplevel, :load-toplevel or :execute, \
2010 but got:" (car w))]))]
2011 [situ (vm-eval-situation)])
2012 (when (and (eqv? situ SCM_VM_COMPILING)
2013 (memq :compile-toplevel wlist)
2014 (cenv-toplevel? cenv))
2015 (dolist [e expr] (eval e (cenv-module cenv))))
2016 (if (or (and (eqv? situ SCM_VM_LOADING)
2017 (memq :load-toplevel wlist)
2018 (cenv-toplevel? cenv))
2019 (and (eqv? situ SCM_VM_EXECUTING)
2020 (memq :execute wlist)))
2021 ($seq (imap (cut pass1 <> cenv) expr))
2022 ($const-undef)))]
2023 [_ (error "syntax-error: malformed eval-when:" form)]))
2024
2025 #|
2026 (define-pass1-syntax (with-meta form cenv) :gauche
2027 (match form
2028 [(_ (meta ...) expr)
2029 (let1 exp (pass1 expr cenv)
2030 exp)]
2031 [_ (error "syntax-error: malformed with-meta:" form)]))
2032 |#