1 ;;; Continuation-passing style (CPS) intermediate language (IL)
3 ;; Copyright (C) 2013, 2014 Free Software Foundation, Inc.
5 ;;;; This library is free software; you can redistribute it and/or
6 ;;;; modify it under the terms of the GNU Lesser General Public
7 ;;;; License as published by the Free Software Foundation; either
8 ;;;; version 3 of the License, or (at your option) any later version.
10 ;;;; This library is distributed in the hope that it will be useful,
11 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ;;;; Lesser General Public License for more details.
15 ;;;; You should have received a copy of the GNU Lesser General Public
16 ;;;; License along with this library; if not, write to the Free Software
17 ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 (define-module (language cps verify)
25 #:use-module (ice-9 match)
26 #:use-module (srfi srfi-26)
27 #:use-module (language cps)
28 #:export (verify-cps))
30 (define (verify-cps fun)
31 (define seen-gensyms (make-hash-table))
34 (if (hashq-ref seen-gensyms sym)
35 (error "duplicate gensym" sym)
37 (hashq-set! seen-gensyms sym #t)
40 (define (add-env new env)
43 (add-env (cdr new) (add (car new) env))))
45 (define (check-var sym env)
47 ((not (hashq-ref seen-gensyms sym))
48 (error "unbound lexical" sym))
50 (error "displaced lexical" sym))))
52 (define (check-src src)
53 (if (and src (not (and (list? src) (and-map pair? src)
54 (and-map symbol? (map car src)))))
57 (define (visit-cont-body cont k-env v-env)
62 (($ $kreceive ($ $arity ((? symbol?) ...) () (or #f (? symbol?)) () #f) k)
64 (($ $kargs ((? symbol? name) ...) ((? symbol? sym) ...) body)
65 (unless (= (length name) (length sym))
66 (error "name and sym lengths don't match" name sym))
67 (visit-term body k-env (add-env sym v-env)))
69 ;; $kclause, $kentry, and $ktail are only ever seen in $fun.
70 (error "unexpected cont body" cont))))
72 (define (visit-clause clause k-env v-env)
79 (and rest (or #f (? symbol?)))
80 (((? keyword? kw) (? symbol? kwname) (? symbol? kwsym)) ...)
82 ($ $cont kbody (and body ($ $kargs names syms _)))))
83 (for-each (lambda (sym)
84 (unless (memq sym syms)
85 (error "bad keyword sym" sym)))
87 ;; FIXME: It is technically possible for kw syms to alias other
89 (unless (equal? (append req opt (if rest (list rest) '()) kwname)
91 (error "clause body names do not match arity names" exp))
92 (let ((k-env (add-env (list kclause kbody) k-env)))
93 (visit-cont-body body k-env v-env)))
95 (error "unexpected clause" clause))))
97 (define (visit-fun fun k-env v-env)
99 (($ $fun src meta ((? symbol? free) ...)
101 ($ $kentry (? symbol? self) ($ $cont ktail ($ $ktail)) clauses)))
102 (when (and meta (not (and (list? meta) (and-map pair? meta))))
103 (error "meta should be alist" meta))
104 (for-each (cut check-var <> v-env) free)
106 ;; Reset the continuation environment, because Guile's
107 ;; continuations are local.
108 (let ((v-env (add-env (list self) v-env))
109 (k-env (add-env (list ktail) '())))
110 (for-each (cut visit-clause <> k-env v-env) clauses)))
112 (error "unexpected $fun" fun))))
114 (define (visit-expression exp k-env v-env)
120 (($ $prim (? symbol? name))
123 (visit-fun exp k-env v-env))
124 (($ $call (? symbol? proc) ((? symbol? arg) ...))
125 (check-var proc v-env)
126 (for-each (cut check-var <> v-env) arg))
127 (($ $callk (? symbol? k*) (? symbol? proc) ((? symbol? arg) ...))
128 ;; We don't check that k* is in scope; it's actually inside some
129 ;; other function, probably. We rely on the transformation that
130 ;; introduces the $callk to be correct, and the linker to resolve
132 (check-var proc v-env)
133 (for-each (cut check-var <> v-env) arg))
134 (($ $primcall (? symbol? name) ((? symbol? arg) ...))
135 (for-each (cut check-var <> v-env) arg))
136 (($ $values ((? symbol? arg) ...))
137 (for-each (cut check-var <> v-env) arg))
138 (($ $prompt escape? tag handler)
139 (unless (boolean? escape?) (error "escape? should be boolean" escape?))
140 (check-var tag v-env)
141 (check-var handler k-env))
143 (error "unexpected expression" exp))))
145 (define (visit-term term k-env v-env)
147 (($ $letk (($ $cont (? symbol? k) cont) ...) body)
148 (let ((k-env (add-env k k-env)))
149 (for-each (cut visit-cont-body <> k-env v-env) cont)
150 (visit-term body k-env v-env)))
152 (($ $letrec ((? symbol? name) ...) ((? symbol? sym) ...) (fun ...) body)
153 (unless (= (length name) (length sym) (length fun))
154 (error "letrec syms, names, and funs not same length" term))
155 (let ((v-env (add-env sym v-env)))
156 (for-each (cut visit-fun <> k-env v-env) fun)
157 (visit-term body k-env v-env)))
159 (($ $continue k src exp)
162 (visit-expression exp k-env v-env))
165 (error "unexpected term" term))))
167 (visit-fun fun '() '())