adding while loops if we can trivially detect them.

This commit is contained in:
Danny Yoo 2012-02-28 20:08:20 -05:00
parent e62b7e9977
commit d3ab24d6c8

View File

@ -222,19 +222,41 @@ EOF
(: default-assemble-basic-block (BasicBlock Blockht (Setof Symbol) (Setof Symbol) Output-Port -> 'ok)) (: default-assemble-basic-block (BasicBlock Blockht (Setof Symbol) (Setof Symbol) Output-Port -> 'ok))
(define (default-assemble-basic-block a-basic-block blockht entry-points function-entry-and-exit-names op) (define (default-assemble-basic-block a-basic-block blockht entry-points function-entry-and-exit-names op)
(fprintf op "var ~a=function(M){"
(assemble-label (make-Label (BasicBlock-name a-basic-block)) blockht))
(define is-self-looping?
(let ()
(cond [(not (empty? (BasicBlock-stmts a-basic-block)))
(define last-stmt
(last (BasicBlock-stmts a-basic-block)))
(cond
[(Goto? last-stmt)
(define target (Goto-target last-stmt))
(equal? target (make-Label (BasicBlock-name a-basic-block)))]
[else #f])]
[else #f])))
(cond (cond
[(set-contains? function-entry-and-exit-names (BasicBlock-name a-basic-block)) [is-self-looping?
(fprintf op "var ~a=function(M){if(--M.cbt<0){throw ~a;}\n" (fprintf op "while(true){")
(assemble-label (make-Label (BasicBlock-name a-basic-block)) blockht) (when (set-contains? function-entry-and-exit-names (BasicBlock-name a-basic-block))
(assemble-label (make-Label (BasicBlock-name a-basic-block)) blockht))] (fprintf op "if(--M.cbt<0){throw ~a;}\n"
(assemble-label (make-Label (BasicBlock-name a-basic-block)) blockht)))
(assemble-block-statements (BasicBlock-name a-basic-block)
(drop-right (BasicBlock-stmts a-basic-block) 1)
blockht
entry-points
op)
(fprintf op "}")]
[else [else
(fprintf op "var ~a=function(M){\n" (when (set-contains? function-entry-and-exit-names (BasicBlock-name a-basic-block))
(assemble-label (make-Label (BasicBlock-name a-basic-block)) blockht))]) (fprintf op "if(--M.cbt<0){throw ~a;}\n"
(assemble-block-statements (BasicBlock-name a-basic-block) (assemble-label (make-Label (BasicBlock-name a-basic-block)) blockht)))
(BasicBlock-stmts a-basic-block) (assemble-block-statements (BasicBlock-name a-basic-block)
blockht (BasicBlock-stmts a-basic-block)
entry-points blockht
op) entry-points
op)])
(display "};\n" op) (display "};\n" op)
'ok) 'ok)
@ -246,9 +268,9 @@ EOF
(: default (UnlabeledStatement -> 'ok)) (: default (UnlabeledStatement -> 'ok))
(define (default stmt) (define (default stmt)
(when (and (empty? (rest stmts)) ;(when (and (empty? (rest stmts))
(not (Goto? stmt))) ; (not (Goto? stmt)))
(log-debug (format "Last statement of the block ~a is not a goto" name))) ; (log-debug (format "Last statement of the block ~a is not a goto" name)))
(display (assemble-statement stmt blockht) op) (display (assemble-statement stmt blockht) op)
(newline op) (newline op)