Skip to content

Commit 4363e5b

Browse files
committed
turned on optimize-if and fixed some bugs in that code
1 parent 0bac8fe commit 4363e5b

File tree

3 files changed

+13
-51
lines changed

3 files changed

+13
-51
lines changed

conditionals.rkt

+7-42
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@
123123
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
124124
;; flatten : S1 -> C1-expr x (C1-stmt list)
125125

126-
(field [optimize-if #f])
126+
(field [optimize-if #t])
127127

128128
(define/public (flatten-if new-thn thn-ss new-els els-ss xs)
129129
(lambda (cnd)
@@ -143,7 +143,7 @@
143143
(append e-ss
144144
`((assign ,x ,new-e))
145145
body-ss)
146-
(append xs1 xs2))]
146+
(cons x (append xs1 xs2)))]
147147
[`(not ,cnd) #:when optimize-if
148148
((flatten-if new-els els-ss new-thn thn-ss xs) cnd)]
149149
[`(,cmp ,e1 ,e2)
@@ -179,14 +179,7 @@
179179
(lambda (e)
180180
(verbose "flatten" e)
181181
(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-
188182
[(? boolean?) (values e '() '())]
189-
190183
[`(and ,e1 ,e2)
191184
(define-values (new-e1 e1-ss xs1) ((flatten #t) e1))
192185
(define-values (new-e2 e2-ss xs2) ((flatten #f) e2))
@@ -198,37 +191,12 @@
198191
((assign ,tmp #f)))))
199192
(cons tmp (append xs1 xs2))
200193
)]
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-
221194
[`(if ,cnd ,thn ,els)
222195
(define-values (new-thn thn-ss xs1) ((flatten #t) thn))
223196
(define-values (new-els els-ss xs2) ((flatten #t) els))
224197
((flatten-if new-thn thn-ss new-els els-ss (append xs1 xs2)) cnd)]
225-
226198
[`(has-type ,e1 ,t)
227199
((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))))]
232200
[`(program (type ,ty) ,body)
233201
(define-values (new-body ss xs) ((flatten #t) body))
234202
`(program ,xs (type ,ty)
@@ -291,11 +259,9 @@
291259
(define new-e2 ((select-instructions) e1))
292260
;; second operand of cmpq can't be an immediate
293261
(define comparison
294-
(cond [(and (immediate? new-e1) (immediate? new-e2))
262+
(cond [(immediate? new-e2)
295263
`((movq ,new-e2 (reg rax))
296264
(cmpq ,new-e1 (reg rax)))]
297-
[(immediate? new-e2)
298-
`((cmpq ,new-e2 ,new-e1))]
299265
[else
300266
`((cmpq ,new-e1 ,new-e2))]))
301267
;; This works because movzbq %al, %rax is a valid instruction
@@ -311,9 +277,8 @@
311277
[els-ss (append* (map (select-instructions)
312278
els-ss))])
313279
`((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))]
317282
[`(program ,locals (type ,ty) ,ss ...)
318283
(let ([new-ss (map (select-instructions) ss)])
319284
`(program ,locals (type ,ty) ,@(append* new-ss)))]
@@ -326,8 +291,8 @@
326291
(define/override (free-vars a)
327292
(match a
328293
[`(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))]
331296
[else (super free-vars a)]
332297
))
333298

interp.rkt

+4-7
Original file line numberDiff line numberDiff line change
@@ -363,13 +363,10 @@
363363
((interp-x86 (cons (cons x v) env)) ss)]
364364
[`((jmp ,label) . ,ss)
365365
((interp-x86 env) (goto-label label (program)))]
366-
[`((jmp-if e ,label) . ,ss)
367-
(let* ([eflags (lookup '__flag env)]
368-
[zero (bitwise-and #b1000000 eflags)]
369-
[zero? (i2b (arithmetic-shift zero -6))])
370-
(cond [zero?
371-
((interp-x86 env) (goto-label label (program)))]
372-
[else ((interp-x86 env) ss)]))]
366+
[`((jmp-if ,cc ,label) . ,ss)
367+
(cond [(eq? (eflags-status env cc) 1)
368+
((interp-x86 env) (goto-label label (program)))]
369+
[else ((interp-x86 env) ss)])]
373370
[`(program ,xs (type ,ty) ,ss ...)
374371
(display-by-type ty ((interp-x86 env) `(program ,xs ,@ss)))]
375372
[`(program ,xs ,ss ...)

vectors.rkt

+2-2
Original file line numberDiff line numberDiff line change
@@ -159,15 +159,15 @@
159159
(values new-thn thn-ss xs)]
160160
[#f #:when optimize-if
161161
(values new-els els-ss xs)]
162-
[`(let ([,x ,e]) ,body) #:when optimize-if
162+
[`(let ([,x (has-type ,e ,e-type)]) ,body) #:when optimize-if
163163
(define-values (new-e e-ss xs1) ((flatten #f) e))
164164
(define-values (new-body body-ss xs2)
165165
((flatten-if if-type new-thn thn-ss new-els els-ss xs) body))
166166
(values new-body
167167
(append e-ss
168168
`((assign ,x ,new-e))
169169
body-ss)
170-
(append xs1 xs2))]
170+
(cons (cons x e-type) (append xs1 xs2)))]
171171
[`(not ,cnd) #:when optimize-if
172172
((flatten-if if-type new-els els-ss new-thn thn-ss xs) cnd)]
173173
[`(,cmp ,e1 ,e2)

0 commit comments

Comments
 (0)