Datasets:

License:
File size: 11,948 Bytes
3dcad1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
;;; Continuation-passing style (CPS) intermediate language (IL)

;; Copyright (C) 2021,2023 Free Software Foundation, Inc.

;;;; This library is free software; you can redistribute it and/or
;;;; modify it under the terms of the GNU Lesser General Public
;;;; License as published by the Free Software Foundation; either
;;;; version 3 of the License, or (at your option) any later version.
;;;;
;;;; This library is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;;; Lesser General Public License for more details.
;;;;
;;;; You should have received a copy of the GNU Lesser General Public
;;;; License along with this library; if not, write to the Free Software
;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

;;; Commentary:
;;;
;;; Helper facilities for working with CPS.
;;;
;;; Code:

(define-module (language cps dump)
  #:use-module (ice-9 format)
  #:use-module (ice-9 match)
  #:use-module (srfi srfi-1)
  #:use-module (language cps)
  #:use-module (language cps intset)
  #:use-module (language cps intmap)
  #:use-module (language cps graphs)
  #:use-module (language cps utils)
  #:export (dump))

;; ideas: unused vars print as _
;;        print all labels
;;        call bb headers with values
;;        annotate blocks with available bindings?  live bindings?
;;        how to print calls...
;;        dot graph

(define (cont-successors cont)
  (match cont
    (($ $kargs _ _ term)
     (match term
       (($ $continue k) (list k))
       (($ $branch kf kt) (list kf kt))
       (($ $switch kf kt*) (cons kf kt*))
       (($ $prompt k kh) (list k kh))
       (($ $throw) '())))
    (($ $kclause _ kbody kalternate)
     (if kalternate
         (list kbody kalternate)
         (list kbody)))
    (($ $kfun src meta self ktail kentry)
     (list ktail kentry))
    (($ $kreceive arity kargs) (list kargs))
    (($ $ktail) '())))

(define (compute-block-entries cps kfun body all-labels?)
  (if all-labels?
      body
      (let ((preds (compute-predecessors cps kfun #:labels body)))
        ;; Conts whose predecessor count is not 1 start blocks.
        (define (add-entry label blocks)
          (match (intmap-ref preds label)
            ((_) blocks)
            (_ (intset-add! blocks label))))
        ;; Continuations of branches start blocks.
        (define (add-exits label blocks)
          (fold1 (lambda (succ blocks)
                   (intset-add! blocks succ))
                 (match (cont-successors (intmap-ref cps label))
                   ((_) '())
                   (succs succs))
                 blocks))
        (persistent-intset
         (intset-fold
          (lambda (label blocks)
            (add-exits label (add-entry label blocks)))
          body
          empty-intset)))))

(define (collect-blocks cps entries)
  (define (collect-block entry)
    (let ((cont (intmap-ref cps entry)))
      (acons entry cont
             (match (cont-successors (intmap-ref cps entry))
               ((succ)
                (if (intset-ref entries succ)
                    '()
                    (collect-block succ)))
               (_ '())))))
  (persistent-intmap
   (intset-fold
    (lambda (start blocks)
      (intmap-add! blocks start (collect-block start)))
    entries
    empty-intmap)))

(define (compute-block-succs blocks)
  (intmap-map (lambda (entry conts)
                (match conts
                  (((_ . _) ... (exit . cont))
                   (fold1 (lambda (succ succs)
                            (intset-add succs succ))
                          (cont-successors cont)
                          empty-intset))))
              blocks))

(define (dump-block cps port labelled-conts)
  (define (format-label label) (format #f "L~a" label))
  (define (format-name name) (if name (symbol->string name) "_"))
  (define (format-var var) (format #f "v~a" var))
  (define (format-loc src)
    (match src
      (#f #f)
      (#(filename line column)
       (format #f "~a:~a:~a"
               (or filename "<unknown>")
               (1+ line)
               column))))
  (define (arg-list strs) (string-join strs ", "))
  (define (false-if-empty str) (if (string-null? str) #f str))
  (define (format-arity arity)
    (match arity
      (($ $arity req opt rest kw aok?)
       (arg-list
        `(,@(map format-name req)
          ,@(map (lambda (name)
                   (format #f "[~a]" (format-name name)))
                 opt)
          ,@(map (match-lambda
                   ((kw name var)
                    (format #f "~a" kw)))
                 kw)
          ,@(if aok? '("[#:allow-other-keys]") '())
          ,@(if rest
                (list (string-append (format-name rest) "..."))
                '()))))))
  (define (format-primcall op param args)
    (format #f "~a~@[[~s]~](~a)" op param (arg-list (map format-var args))))
  (define (format-exp exp)
    (match exp
      (($ $const val)
       (format #f "const ~s" val))
      (($ $prim name)
       (format #f "prim ~s" name))
      (($ $fun body)
       (format #f "fun ~a" (format-label body)))
      (($ $rec names syms funs)
       (format #f "rec(~a)" (arg-list (map format-exp funs))))
      (($ $const-fun label)
       (format #f "const-fun ~a" (format-label label)))
      (($ $code label)
       (format #f "code ~a" (format-label label)))
      (($ $call proc args)
       (format #f "call ~a(~a)"
               (format-var proc) (arg-list (map format-var args))))
      (($ $callk k proc args)
       (format #f "callk ~a(~a)" (format-label k)
               (arg-list
                (cons (if proc (format-var proc) "_")
                      (map format-var args)))))
      (($ $calli args callee)
       (format #f "calli ~a(~a)"
               (format-var callee) (arg-list (map format-var args))))
      (($ $primcall name param args)
       (format-primcall name param args))
      (($ $values args)
       (arg-list (map format-var args)))))
  (define (dump-annotation ann src)
    (when (or ann src)
      (format port "~45t ; ~@[~a ~]" ann)
      (when src
        (let* ((src (format-loc src))
               (col (- 80 4 (string-length src))))
          (format port "~vt at ~a" col src))))
    (newline port))
  (define (dump-definition src names vars fmt . args)
    (define (take formatter val)
      (cond
       ((not val) #f)
       ((string? val) (false-if-empty val))
       ((null? val) #f)
       (else (arg-list (map formatter val)))))
    (let ((names (take format-name names))
          (vars (take format-var vars)))
      (format port "  ~@[~a := ~]~?" vars fmt args)
      (dump-annotation names src)))
  (define (dump-statement src ann fmt . args)
    (format port "  ~?" fmt args)
    (dump-annotation (and ann (false-if-empty ann)) src))
  (define (dump-block-header label cont)
    (match cont
      (($ $kargs names vars)
       (format port "~a(~a):"
               (format-label label)
               (arg-list (map format-var vars)))
       (dump-annotation (false-if-empty (arg-list (map format-name names)))
                        #f))
      (($ $ktail)
       (values))
      (($ $kfun src meta self ktail kentry)
       (let ((name (assq-ref meta 'name)))
         (format port "~a:" (format-label label))
         (dump-annotation name src)))
      ((or ($ $kreceive) ($ $kclause))
       (format port "~a:\n" (format-label label)))))
  (define (dump-block-body label cont)
    (match cont
      (($ $kargs _ _ ($ $continue k src exp))
       (match (intmap-ref cps k)
         (($ $kargs names vars)
          (dump-definition src names vars "~a" (format-exp exp)))
         (_
          (dump-definition src #f #f "~a" (format-exp exp)))))
      (($ $kreceive arity kargs)
       (match (intmap-ref cps kargs)
         (($ $kargs names vars)
          (dump-definition #f names vars
                           "receive(~a)" (format-arity arity)))))
      (($ $ktail)
       (values))
      (($ $kclause arity kbody #f)
       (match (intmap-ref cps kbody)
         (($ $kargs names vars)
          (dump-definition #f names vars
                           "receive(~a)" (format-arity arity)))))))
  (define (dump-block-exit label cont)
    (match cont
      (($ $kargs _ _ term)
       (match term
         (($ $continue k src exp)
          (match (intmap-ref cps k)
            (($ $ktail)
             (match exp
               (($ $values vals)
                (dump-statement src #f
                                "return ~a" (arg-list (map format-var vals))))
               (_
                (dump-statement src #f
                                "tail ~a" (format-exp exp)))))
            (_
             (dump-statement src #f
                             "~a(~a)" (format-label k) (format-exp exp)))))
         (($ $branch kf kt src op param args)
          (dump-statement src #f
                          "~a ? ~a() : ~a()"
                          (format-primcall op param args)
                          (format-label kt)
                          (format-label kf)))
         (($ $switch kf kt* src arg)
          (dump-statement src #f
                          "[~a]~a() or ~a()"
                          (arg-list (map format-label kt*))
                          (format-var arg)
                          (format-label kf)))
         (($ $prompt k kh src escape? tag)
          (dump-statement src #f
                          "~a(prompt(kh:~a,~a tag:~a)"
                          (format-label k)
                          (format-label kh)
                          (if escape? ", escape-only" "")
                          (format-var tag)))
         (($ $throw src op param args)
          (dump-statement src #f
                          "throw ~a" (format-primcall op param args)))))
      (($ $kreceive arity kargs)
       (dump-statement #f #f
                       "~a(receive(~a))"
                       (format-label kargs)
                       (format-arity arity)))
      (($ $kfun src meta self ktail kentry)
       (for-each (match-lambda
                   ((k . v)
                    (unless (eq? k 'name)
                      (format port "  meta: ~a: ~s\n" k v))))
                 meta)
       ;; (format port "  tail: ~a:\n" (format-label ktail))
       (when self
         (format port "  ~a := self\n" (format-var self)))
       (format port "  ~a(...)\n" (format-label kentry)))
      (($ $kclause arity kbody kalt)
       (dump-statement #f #f
                       "~a(receive(~a))~@[or ~a()~]\n"
                       (format-label kbody)
                       (format-arity arity)
                       (and=> kalt format-label)))
      (($ $ktail)
       (values))))
  (match labelled-conts
    (((label . cont) . _)
     (dump-block-header label cont)))
  (let lp ((labelled-conts labelled-conts))
    (match labelled-conts
      (((label . cont))
       (dump-block-exit label cont))
      (((label . cont) . labelled-conts)
       (dump-block-body label cont)
       (lp labelled-conts)))))

(define (dump-function cps port kfun body all-labels?)
  (define entries (compute-block-entries cps kfun body all-labels?))
  (define blocks (collect-blocks cps entries))
  (define block-succs (compute-block-succs blocks))
  (define block-order (compute-reverse-post-order block-succs kfun))
  (for-each (lambda (entry)
              (dump-block cps port (intmap-ref blocks entry)))
            block-order)
  (values))

(define* (dump cps #:key
               (port (current-output-port))
               (entry (intmap-next cps))
               (all-labels? #f))
  (let ((functions (compute-reachable-functions cps entry)))
    (intmap-fold (lambda (kfun body)
                   (unless (eqv? kfun entry) (newline port))
                   (dump-function cps port kfun body all-labels?))
                 functions)))