summaryrefslogtreecommitdiff
path: root/chickadee/graphics/seagull/cps.scm
blob: 3c2f1fe94bca397f1d309a2e06d8482885d255ab (plain)
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
;;; Chickadee Game Toolkit
;;; Copyright © 2023 David Thompson <dthompson2@worcester.edu>
;;;
;;; Licensed under the Apache License, Version 2.0 (the "License");
;;; you may not use this file except in compliance with the License.
;;; You may obtain a copy of the License at
;;;
;;;    http://www.apache.org/licenses/LICENSE-2.0
;;;
;;; Unless required by applicable law or agreed to in writing, software
;;; distributed under the License is distributed on an "AS IS" BASIS,
;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;;; See the License for the specific language governing permissions and
;;; limitations under the License.

;; A simple form of a continuation-passing style intermediate
;; language.  This intermediate representation provides a control flow
;; graph.  Inspired by Guile's CPS soup but simplified because our
;; compilation target is a lot simpler than what a general purpose
;; language needs to handle and also I'm a compiler baby and not a
;; genius like Andy Wingo.  Unlike Guile's CPS, Seagull's CPS is
;; statically typed.  The initial CPS conversion pass leaves the type
;; annotations blank but the type inference pass must ultimately be
;; able to assign types to all variables and functions or else the
;; program is not valid.
(define-module (chickadee graphics seagull cps)
  #:use-module (chickadee graphics seagull types)
  #:use-module (chickadee graphics seagull utils)
  #:use-module (ice-9 match)
  #:use-module (language cps intmap)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-9)
  #:use-module (srfi srfi-9 gnu)
  #:export (<arguments>
            make-arguments
            arguments?
            arguments-names
            arguments-term
            arguments-types

            <function-entry>
            make-function-entry
            function-entry?
            function-entry-source
            function-entry-params
            function-entry-results
            function-entry-start
            function-entry-return
            function-entry-type

            <continue>
            make-continue
            continue?
            continue-source
            continue-label
            continue-expression

            <return>
            make-return
            return?
            return-outputs

            <branch>
            make-branch
            branch?
            branch-source
            branch-name
            branch-consequent
            branch-alternate

            <discard>
            make-discard
            discard?
            discard-source

            <cps-constant>
            make-cps-constant
            cps-constant?
            cps-constant-value
            cps-constant-type

            <cps-primitive-call>
            make-cps-primitive-call
            cps-primitive-call?
            cps-primitive-call-operator
            cps-primitive-call-arguments
            cps-primitive-call-types

            <cps-call>
            make-cps-call
            cps-call?
            cps-call-function
            cps-call-arguments
            cps-call-type

            <cps-values>
            make-cps-values
            cps-values?
            cps-values-arguments

            <cps-assignment>
            make-cps-assignment
            cps-assignment?
            cps-assignment-variable
            cps-assignment-value

            <cps-function>
            make-cps-function
            cps-function?
            cps-function-body

            continuation?
            term?
            expression?
            cps->sexp
            sexp->cps
            graph->sexp
            sexp->graph))

;; Continuations
(define-record-type <arguments>
  (make-arguments names term types)
  arguments?
  (names arguments-names)
  (term arguments-term)
  (types arguments-types))

(define-record-type <function-entry>
  (make-function-entry source params results start return type)
  function-entry?
  (source function-entry-source)
  (params function-entry-params)
  (results function-entry-results)
  (start function-entry-start)
  (return function-entry-return)
  (type function-entry-type))

(define (continuation? obj)
  (or (arguments? obj)
      (function-entry? obj)))

;; Terms
(define-record-type <continue>
  (make-continue source label expression)
  continue?
  (source continue-source)
  (label continue-label)
  (expression continue-expression))

(define-record-type <return>
  (make-return outputs)
  return?
  (outputs return-outputs))

(define-record-type <branch>
  (make-branch source name consequent alternate)
  branch?
  (source branch-source)
  (name branch-name)
  (consequent branch-consequent)
  (alternate branch-alternate))

;; Discard is available only in fragment shaders and is used to
;; terminate early and throw away the fragment, which means it has no
;; continuation.
(define-record-type <discard>
  (make-discard source)
  discard?
  (source discard-source))

(define (term? obj)
  (or (continue? obj)
      (branch? obj)
      (discard? obj)))

;; Expressions (prefixed with 'cps' to distinguish them from the
;; tree-based IL when both modules are imported)
(define-record-type <cps-constant>
  (make-cps-constant value type)
  cps-constant?
  (value cps-constant-value)
  ;; TODO: Unnecessary. Delete it.
  (type cps-constant-type))

(define-record-type <cps-primitive-call>
  (make-cps-primitive-call operator arguments types)
  cps-primitive-call?
  (operator cps-primitive-call-operator)
  (arguments cps-primitive-call-arguments)
  ;; TODO: Unused. Delete it.
  (types cps-primitive-call-types))

(define-record-type <cps-call>
  (make-cps-call function arguments types)
  cps-call?
  (function cps-call-function)
  (arguments cps-call-arguments)
  ;; TODO: Unused. Delete it.
  (types cps-call-types))

(define-record-type <cps-values>
  (make-cps-values arguments)
  cps-values?
  (arguments cps-values-arguments))

(define-record-type <cps-assignment>
  (make-cps-assignment variable value)
  cps-assignment?
  (variable cps-assignment-variable)
  (value cps-assignment-value))

(define-record-type <cps-function>
  (make-cps-function body)
  cps-function?
  (body cps-function-body))

(define (expression? obj)
  (or (cps-constant? obj)
      (cps-primitive-call? obj)
      (cps-call? obj)
      (cps-values? obj)))

(define (cps->sexp exp)
  (match exp
    (($ <arguments> names term types)
     `(arguments ,names ,(cps->sexp term)))
    (($ <function-entry> _ params results start return _)
     `(function-entry ,params ,results ,start ,return))
    (($ <return> outputs)
     `(return ,@outputs))
    (($ <continue> _ label exp)
     `(continue ,label ,(cps->sexp exp)))
    (($ <branch> _ name consequent alternate)
     `(branch ,name ,consequent ,alternate))
    (($ <discard> _)
     '(discard))
    (($ <cps-constant> val _)
     `(constant ,val))
    (($ <cps-primitive-call> op args _)
     `(primitive-call ,op ,args))
    (($ <cps-call> f args _)
     `(call ,f ,args))
    (($ <cps-values> args)
     `(values ,@args))
    (($ <cps-assignment> var val)
     `(assignment ,var ,val))
    (($ <cps-function> body)
     `(function ,body))))

(define (graph->sexp graph)
  (intmap-fold-right (lambda (k cont memo)
                       (alist-cons k (cps->sexp cont) memo))
                     graph '()))

(define (sexp->cps exp)
  (match exp
    (('arguments ((? exact-integer? names) ...) term)
     (make-arguments names (sexp->cps term) #f))
    (('function-entry (? exact-integer? name)
                      ((? exact-integer? params) ...)
                      ((? exact-integer? results) ...)
                      (? exact-integer? start)
                      (? exact-integer? return))
     (make-function-entry #f params results start return #f))
    (('continue (and (or (? exact-integer?) #f) label) exp)
     (make-continue #f label (sexp->cps exp)))
    (('return outputs ...)
     (make-return outputs))
    (('branch (? exact-integer? name)
              (? exact-integer? consequent)
              (? exact-integer? alternate))
     (make-branch #f name consequent alternate))
    (('discard)
     (make-discard #f))
    (('constant val)
     (make-cps-constant val (type-for-constant val)))
    (('primitive-call (? symbol? op) ((? exact-integer? args) ...))
     (make-cps-primitive-call op args (list (fresh-type-variable))))
    (('call (? exact-integer? f) ((? exact-integer? args) ...))
     (make-cps-call f args (list (fresh-type-variable))))
    (('values (? exact-integer? args) ...)
     (make-cps-values args))
    (('assignment (? exact-integer? var) (? exact-integer? val))
     (make-cps-assignment var val))))

(define (sexp->graph exp)
  (fold (lambda (pair graph)
          (match pair
            (((? exact-integer? k) . exp*)
             (intmap-add graph k (sexp->cps exp*)))))
        empty-intmap exp))

(define (print-cps cps port)
  (format port "<cps ~a>" (cps->sexp cps)))

(set-record-type-printer! <arguments> print-cps)
(set-record-type-printer! <function-entry> print-cps)
(set-record-type-printer! <return> print-cps)
(set-record-type-printer! <continue> print-cps)
(set-record-type-printer! <branch> print-cps)
(set-record-type-printer! <discard> print-cps)
(set-record-type-printer! <cps-constant> print-cps)
(set-record-type-printer! <cps-primitive-call> print-cps)
(set-record-type-printer! <cps-call> print-cps)
(set-record-type-printer! <cps-values> print-cps)
(set-record-type-printer! <cps-function> print-cps)