-
Notifications
You must be signed in to change notification settings - Fork 1
/
rco.rkt
155 lines (150 loc) · 6.13 KB
/
rco.rkt
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
#lang racket
(require "utilities.rkt"
"utils.rkt")
(provide remove-complex-opera*)
; returns atomic expression, new env
(define (rco-atm e)
(match e
[(Void) (values (Void) '())]
[(Int i) (values (Int i) '())]
[(Var v) (values (Var v) '())]
[(Bool b) (values (Bool b) '())]
[(HasType e t) #:when (atom? e)
(values (HasType e t) '())]
[(HasType e t)
(let-values ([(atm env) (rco-atm e)])
; Invariant : atm is *always* a (gensym 'tmp), since other cases are
; caught by previous clauses
(values (HasType (Var atm) t)
(dict-set env atm (HasType (dict-ref env atm) t))))]
[(Collect bytes)
(let ([tmp (gensym 'tmp)])
(values tmp `((,tmp . ,(Collect bytes)))))]
[(GlobalValue var)
(let ([tmp (gensym 'tmp)])
(values tmp `((,tmp . ,(GlobalValue var)))))]
[(Allocate bytes t)
(let ([tmp (gensym 'tmp)])
(values tmp `((,tmp . ,(Allocate bytes t)))))]
[(Apply fun arg*)
(let* ([tmp (gensym 'tmp)]
[fun-atm-env ((compose (lambda (atm env)
(cons atm env))
rco-atm) fun)]
[fun-atm (car fun-atm-env)]
[fun-atm (if (symbol? fun-atm)
(Var fun-atm)
fun-atm)]
[fun-env (cdr fun-atm-env)]
[arg-atm-env* (map (compose (lambda (atm env)
(cons atm env))
rco-atm)
arg*)]
[arg-env* (filter-map (lambda (x)
(and (not (empty? (cdr x)))
(cdr x))) arg-atm-env*)]
[arg-atm* (map (compose (lambda (atm)
(if (symbol? atm)
(Var atm)
atm))
car)
arg-atm-env*)])
(values tmp (append (apply append (cons fun-env arg-env*))
`((,tmp . ,(Apply fun-atm arg-atm*))))))]
[(FunRef f)
(let ([tmp (gensym 'funref)])
(values tmp `((,tmp . ,(FunRef f)))))]
[(Prim op es)
(let* ([tmp (gensym 'tmp)]
[atm-envs (map (compose (lambda (atm env)
(cons atm env))
rco-atm)
es)]
; Invariant : alist ordered envs preserve order of execution
[envs (filter-map (lambda (x)
(and (not (empty? (cdr x)))
(cdr x))) atm-envs)]
[atms (map (compose (lambda (atm)
(if (symbol? atm)
(Var atm)
atm))
car)
atm-envs)])
(values tmp (append (apply append envs)
`((,tmp . ,(Prim op atms))))))]
[(If c t e)
(let ([tmp (gensym 'tmp)])
(values tmp `((,tmp . ,(If (rco-exp c) (rco-exp t) (rco-exp e))))))]
[(Let x v b)
(let ([v (rco-exp v)]
[b (rco-exp b)])
(if (atom? b)
(values b `((,x . ,v)))
(let ([tmp (gensym 'tmp)])
(values tmp `((,tmp . ,(Let x (rco-exp v) (rco-exp b))))))))]))
(define (rco-exp e)
(match e
[(Void) (Void)]
[(Int i) (Int i)]
[(Var v) (Var v)]
[(Bool b) (Bool b)]
[(HasType e t) (HasType (rco-exp e) t)]
[(Allocate bytes t) (Allocate bytes t)]
[(Collect bytes) (Collect bytes)]
[(GlobalValue var) (GlobalValue var)]
; Invariant : f is always a symbol
[(FunRef f) (FunRef f)]
[(Apply fun arg*)
(let* ([tmp (gensym 'tmp)]
[fun-atm-env ((compose (lambda (atm env)
(cons atm env))
rco-atm) fun)]
[fun-atm (car fun-atm-env)]
[fun-atm (if (symbol? fun-atm)
(Var fun-atm)
fun-atm)]
[fun-env (cdr fun-atm-env)]
[arg-atm-env* (map (compose (lambda (atm env)
(cons atm env))
rco-atm)
arg*)]
[arg-env* (filter-map (lambda (x)
(and (not (empty? (cdr x)))
(cdr x))) arg-atm-env*)]
[arg-atm* (map (compose (lambda (atm)
(if (symbol? atm)
(Var atm)
atm))
car)
arg-atm-env*)])
(build-lets (apply append (cons fun-env arg-env*))
(Apply fun-atm arg-atm*)))]
[(Prim op es)
(let* ([atm-envs (map (compose (lambda (a e)
(cons a e))
rco-atm)
es)]
[atms (map (compose (lambda (atm)
(if (symbol? atm)
(Var atm)
atm))
car)
atm-envs)]
; Invariant : alist ordered envs preserve order of execution
[envs (filter-map (lambda (x)
(and (not (empty? (cdr x)))
(cdr x)))
atm-envs)])
(build-lets (apply append envs) (Prim op atms)))]
[(If c t e)
(If (rco-exp c) (rco-exp t) (rco-exp e))]
[(Let x v b) (Let x (rco-exp v) (rco-exp b))]))
; arguments of operations are atomic
(define (remove-complex-opera* p)
(match p
[(ProgramDefs info def*)
(ProgramDefs info
(map (lambda (d)
(struct-copy Def d
[body (rco-exp (Def-body d))]))
def*))]))