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-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) (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)
76 (check-label kt k-env)
77 (check-label kf k-env))
78 (($ $kreceive ($ $arity ((? symbol?) ...) () (or #f (? symbol?)) () #f) k)
79 (check-label k k-env))
80 (($ $kargs ((? symbol? name) ...) (sym ...) body)
81 (unless (= (length name) (length sym))
82 (error "name and sym lengths don't match" name sym))
83 (visit-term body k-env (add-vars sym v-env)))
85 ;; $kclause, $kentry, and $ktail are only ever seen in $fun.
86 (error "unexpected cont body" cont))))
88 (define (visit-clause clause k-env v-env)
95 (and rest (or #f (? symbol?)))
96 (((? keyword? kw) (? symbol? kwname) (? symbol? kwsym)) ...)
98 ($ $cont kbody (and body ($ $kargs names syms _)))))
99 (for-each (lambda (sym)
100 (unless (memq sym syms)
101 (error "bad keyword sym" sym)))
103 ;; FIXME: It is technically possible for kw syms to alias other
105 (unless (equal? (append req opt (if rest (list rest) '()) kwname)
107 (error "clause body names do not match arity names" exp))
108 (let ((k-env (add-labels (list kclause kbody) k-env)))
109 (visit-cont-body body k-env v-env)))
111 (error "unexpected clause" clause))))
113 (define (visit-fun fun k-env v-env)
115 (($ $fun src meta (free ...)
117 ($ $kentry self ($ $cont ktail ($ $ktail)) clauses)))
118 (when (and meta (not (and (list? meta) (and-map pair? meta))))
119 (error "meta should be alist" meta))
120 (for-each (cut check-var <> v-env) free)
122 ;; Reset the continuation environment, because Guile's
123 ;; continuations are local.
124 (let ((v-env (add-vars (list self) v-env))
125 (k-env (add-labels (list ktail) '())))
126 (for-each (cut visit-clause <> k-env v-env) clauses)))
128 (error "unexpected $fun" fun))))
130 (define (visit-expression exp k-env v-env)
136 (($ $prim (? symbol? name))
139 (visit-fun exp k-env v-env))
140 (($ $call (? symbol? proc) (arg ...))
141 (check-var proc v-env)
142 (for-each (cut check-var <> v-env) arg))
143 (($ $callk k* proc (arg ...))
144 ;; We don't check that k* is in scope; it's actually inside some
145 ;; other function, probably. We rely on the transformation that
146 ;; introduces the $callk to be correct, and the linker to resolve
148 (check-var proc v-env)
149 (for-each (cut check-var <> v-env) arg))
150 (($ $primcall (? symbol? name) (arg ...))
151 (for-each (cut check-var <> v-env) arg))
152 (($ $values (arg ...))
153 (for-each (cut check-var <> v-env) arg))
154 (($ $prompt escape? tag handler)
155 (unless (boolean? escape?) (error "escape? should be boolean" escape?))
156 (check-var tag v-env)
157 (check-label handler k-env))
159 (error "unexpected expression" exp))))
161 (define (visit-term term k-env v-env)
163 (($ $letk (($ $cont k cont) ...) body)
164 (let ((k-env (add-labels k k-env)))
165 (for-each (cut visit-cont-body <> k-env v-env) cont)
166 (visit-term body k-env v-env)))
168 (($ $letrec ((? symbol? name) ...) (sym ...) (fun ...) body)
169 (unless (= (length name) (length sym) (length fun))
170 (error "letrec syms, names, and funs not same length" term))
171 (let ((v-env (add-vars sym v-env)))
172 (for-each (cut visit-fun <> k-env v-env) fun)
173 (visit-term body k-env v-env)))
175 (($ $continue k src exp)
176 (check-label k k-env)
178 (visit-expression exp k-env v-env))
181 (error "unexpected term" term))))
183 (visit-fun fun '() '())