1 ;;; Continuation-passing style (CPS) intermediate language (IL)
3 ;; Copyright (C) 2013, 2014, 2015 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-labels (make-hash-table))
32 (define seen-vars (make-hash-table))
34 (define (add sym seen env)
35 (when (hashq-ref seen sym)
36 (error "duplicate gensym" sym))
37 (hashq-set! seen sym #t)
40 (define (add-env new seen env)
43 (add-env (cdr new) seen (add (car new) seen env))))
45 (define (add-vars new env)
46 (unless (and-map exact-integer? new)
47 (error "bad vars" new))
48 (add-env new seen-vars env))
50 (define (add-labels new env)
51 (unless (and-map exact-integer? new)
52 (error "bad labels" new))
53 (add-env new seen-labels env))
55 (define (check-ref sym seen env)
57 ((not (hashq-ref seen sym))
58 (error "unbound lexical" sym))
60 (error "displaced lexical" sym))))
62 (define (check-label sym env)
63 (check-ref sym seen-labels env))
65 (define (check-var sym env)
66 (check-ref sym seen-vars env))
68 (define (check-src src)
69 (if (and src (not (and (list? src) (and-map pair? src)
70 (and-map symbol? (map car src)))))
73 (define (visit-cont-body cont k-env v-env)
75 (($ $kreceive ($ $arity ((? symbol?) ...) () (or #f (? symbol?)) () #f) k)
76 (check-label k k-env))
77 (($ $kargs (name ...) (sym ...) body)
78 (unless (= (length name) (length sym))
79 (error "name and sym lengths don't match" name sym))
80 (visit-term body k-env (add-vars sym v-env)))
82 ;; $kclause, $kfun, and $ktail are only ever seen in $fun.
83 (error "unexpected cont body" cont))))
85 (define (visit-clause clause k-env v-env)
92 (and rest (or #f (? symbol?)))
93 (((? keyword? kw) (? symbol? kwname) kwsym) ...)
95 ($ $cont kbody (and body ($ $kargs names syms _)))
97 (for-each (lambda (sym)
98 (unless (memq sym syms)
99 (error "bad keyword sym" sym)))
101 ;; FIXME: It is technically possible for kw syms to alias other
103 (unless (equal? (append req opt (if rest (list rest) '()) kwname)
105 (error "clause body names do not match arity names" exp))
106 (let ((k-env (add-labels (list kclause kbody) k-env)))
107 (visit-cont-body body k-env v-env))
109 (visit-clause alternate k-env v-env)))
111 (error "unexpected clause" clause))))
113 (define (visit-entry entry k-env v-env)
116 ($ $kfun src meta self ($ $cont ktail ($ $ktail)) clause))
117 (when (and meta (not (and (list? meta) (and-map pair? meta))))
118 (error "meta should be alist" meta))
120 ;; Reset the continuation environment, because Guile's
121 ;; continuations are local.
122 (let ((v-env (add-vars (list self) v-env))
123 (k-env (add-labels (list ktail) '())))
125 (visit-clause clause k-env v-env))))
126 (_ (error "unexpected $kfun" entry))))
128 (define (visit-fun fun k-env v-env)
130 (($ $fun (free ...) entry)
131 (for-each (cut check-var <> v-env) free)
132 (visit-entry entry '() v-env))
134 (error "unexpected $fun" fun))))
136 (define (visit-expression exp k-env v-env)
140 (($ $prim (? symbol? name))
145 (visit-fun exp k-env v-env))
146 (($ $call proc (arg ...))
147 (check-var proc v-env)
148 (for-each (cut check-var <> v-env) arg))
149 (($ $callk k* proc (arg ...))
150 ;; We don't check that k* is in scope; it's actually inside some
151 ;; other function, probably. We rely on the transformation that
152 ;; introduces the $callk to be correct, and the linker to resolve
154 (check-var proc v-env)
155 (for-each (cut check-var <> v-env) arg))
156 (($ $branch kt ($ $primcall (? symbol? name) (arg ...)))
158 (for-each (cut check-var <> v-env) arg))
159 (($ $branch kt ($ $values (arg ...)))
161 (for-each (cut check-var <> v-env) arg))
162 (($ $primcall (? symbol? name) (arg ...))
163 (for-each (cut check-var <> v-env) arg))
164 (($ $values (arg ...))
165 (for-each (cut check-var <> v-env) arg))
166 (($ $prompt escape? tag handler)
167 (unless (boolean? escape?) (error "escape? should be boolean" escape?))
168 (check-var tag v-env)
169 (check-label handler k-env))
171 (error "unexpected expression" exp))))
173 (define (visit-term term k-env v-env)
175 (($ $letk (($ $cont k cont) ...) body)
176 (let ((k-env (add-labels k k-env)))
177 (for-each (cut visit-cont-body <> k-env v-env) cont)
178 (visit-term body k-env v-env)))
180 (($ $letrec (name ...) (sym ...) (fun ...) body)
181 (unless (= (length name) (length sym) (length fun))
182 (error "letrec syms, names, and funs not same length" term))
183 (let ((v-env (add-vars sym v-env)))
184 (for-each (cut visit-fun <> k-env v-env) fun)
185 (visit-term body k-env v-env)))
187 (($ $continue k src exp)
188 (check-label k k-env)
190 (visit-expression exp k-env v-env))
193 (error "unexpected term" term))))
195 (visit-entry fun '() '())