|
123 | 123 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
124 | 124 | ;; flatten : S1 -> C1-expr x (C1-stmt list)
|
125 | 125 |
|
126 |
| - (field [optimize-if #f]) |
| 126 | + (field [optimize-if #t]) |
127 | 127 |
|
128 | 128 | (define/public (flatten-if new-thn thn-ss new-els els-ss xs)
|
129 | 129 | (lambda (cnd)
|
|
143 | 143 | (append e-ss
|
144 | 144 | `((assign ,x ,new-e))
|
145 | 145 | body-ss)
|
146 |
| - (append xs1 xs2))] |
| 146 | + (cons x (append xs1 xs2)))] |
147 | 147 | [`(not ,cnd) #:when optimize-if
|
148 | 148 | ((flatten-if new-els els-ss new-thn thn-ss xs) cnd)]
|
149 | 149 | [`(,cmp ,e1 ,e2)
|
|
179 | 179 | (lambda (e)
|
180 | 180 | (verbose "flatten" e)
|
181 | 181 | (match e
|
182 |
| - ;; [`(has-type (void) ,t) |
183 |
| - ;; (values `(void) '() '())] |
184 |
| - ;; [`(has-type ,e1 ,t) |
185 |
| - ;; #:when (or (symbol? e1) (integer? e1) (boolean? e1)) |
186 |
| - ;; (values e1 '() '())] |
187 |
| - |
188 | 182 | [(? boolean?) (values e '() '())]
|
189 |
| - |
190 | 183 | [`(and ,e1 ,e2)
|
191 | 184 | (define-values (new-e1 e1-ss xs1) ((flatten #t) e1))
|
192 | 185 | (define-values (new-e2 e2-ss xs2) ((flatten #f) e2))
|
|
198 | 191 | ((assign ,tmp #f)))))
|
199 | 192 | (cons tmp (append xs1 xs2))
|
200 | 193 | )]
|
201 |
| - |
202 |
| - ;; We override flattening for op's because we |
203 |
| - ;; need to put a has-type on the LHS of the assign. -Jeremy |
204 |
| - ;; [`(has-type (,op ,es ...) ,t) #:when (set-member? (primitives) op) |
205 |
| - ;; (define-values (new-es sss xss) (map3 (flatten #t) es)) |
206 |
| - ;; (define ss (append* sss)) |
207 |
| - ;; (define xs (append* xss)) |
208 |
| - ;; (define prim-apply `(,op ,@new-es)) |
209 |
| - ;; (cond |
210 |
| - ;; [need-atomic |
211 |
| - ;; (define tmp (gensym 'tmp)) |
212 |
| - ;; (values tmp |
213 |
| - ;; (append ss `((assign ,tmp ,prim-apply))) |
214 |
| - ;; (cons tmp xs) )] |
215 |
| - ;; [else (values prim-apply ss xs)])] |
216 |
| - |
217 |
| - ;; For 'let' we just need to strip the enclosing has-type. -Jeremy |
218 |
| - ;; [`(has-type (let ([,x ,rhs]) ,body) ,t) |
219 |
| - ;; ((flatten need-atomic) `(let ([,x ,rhs]) ,body))] |
220 |
| - |
221 | 194 | [`(if ,cnd ,thn ,els)
|
222 | 195 | (define-values (new-thn thn-ss xs1) ((flatten #t) thn))
|
223 | 196 | (define-values (new-els els-ss xs2) ((flatten #t) els))
|
224 | 197 | ((flatten-if new-thn thn-ss new-els els-ss (append xs1 xs2)) cnd)]
|
225 |
| - |
226 | 198 | [`(has-type ,e1 ,t)
|
227 | 199 | ((flatten need-atomic) e1)]
|
228 |
| - |
229 |
| - [`(program ,body) |
230 |
| - (define-values (new-body ss xs) ((flatten #t) body)) |
231 |
| - `(program ,xs ,@(append ss `((return ,new-body))))] |
232 | 200 | [`(program (type ,ty) ,body)
|
233 | 201 | (define-values (new-body ss xs) ((flatten #t) body))
|
234 | 202 | `(program ,xs (type ,ty)
|
|
291 | 259 | (define new-e2 ((select-instructions) e1))
|
292 | 260 | ;; second operand of cmpq can't be an immediate
|
293 | 261 | (define comparison
|
294 |
| - (cond [(and (immediate? new-e1) (immediate? new-e2)) |
| 262 | + (cond [(immediate? new-e2) |
295 | 263 | `((movq ,new-e2 (reg rax))
|
296 | 264 | (cmpq ,new-e1 (reg rax)))]
|
297 |
| - [(immediate? new-e2) |
298 |
| - `((cmpq ,new-e2 ,new-e1))] |
299 | 265 | [else
|
300 | 266 | `((cmpq ,new-e1 ,new-e2))]))
|
301 | 267 | ;; This works because movzbq %al, %rax is a valid instruction
|
|
311 | 277 | [els-ss (append* (map (select-instructions)
|
312 | 278 | els-ss))])
|
313 | 279 | `((if ,cnd ,thn-ss ,els-ss)))]
|
314 |
| - [`(eq? ,a1 ,a2) |
315 |
| - `(eq? ,((select-instructions) a1) |
316 |
| - ,((select-instructions) a2))] |
| 280 | + [`(,cmp ,a1 ,a2) #:when (set-member? (comparison-ops) cmp) |
| 281 | + `(,cmp ,((select-instructions) a1) ,((select-instructions) a2))] |
317 | 282 | [`(program ,locals (type ,ty) ,ss ...)
|
318 | 283 | (let ([new-ss (map (select-instructions) ss)])
|
319 | 284 | `(program ,locals (type ,ty) ,@(append* new-ss)))]
|
|
326 | 291 | (define/override (free-vars a)
|
327 | 292 | (match a
|
328 | 293 | [`(byte-reg ,r) (set (byte-reg->full-reg r))]
|
329 |
| - [`(eq? ,e1 ,e2) (set-union (free-vars e1) |
330 |
| - (free-vars e2))] |
| 294 | + [`(,cmp ,e1 ,e2) #:when (set-member? (comparison-ops) cmp) |
| 295 | + (set-union (free-vars e1) (free-vars e2))] |
331 | 296 | [else (super free-vars a)]
|
332 | 297 | ))
|
333 | 298 |
|
|
0 commit comments