Generate more natural replicator loops when base == 0

This commit is contained in:
Adam Sampson 2007-04-24 13:03:08 +00:00
parent ccda7402e2
commit 7d2013d3f1
2 changed files with 28 additions and 6 deletions

View File

@ -544,17 +544,39 @@ genReplicator rep body
body body
tell ["}\n"] tell ["}\n"]
isZero :: A.Expression -> Bool
isZero (A.ExprLiteral _ (A.Literal _ A.Int (A.IntLiteral _ "0"))) = True
isZero _ = False
genReplicatorLoop :: A.Replicator -> CGen () genReplicatorLoop :: A.Replicator -> CGen ()
genReplicatorLoop (A.For m n base count) genReplicatorLoop (A.For m index base count)
= if isZero base
then genSimpleReplicatorLoop index count
else genGeneralReplicatorLoop index base count
genSimpleReplicatorLoop :: A.Name -> A.Expression -> CGen ()
genSimpleReplicatorLoop index count
= do tell ["int "]
genName index
tell [" = 0; "]
genName index
tell [" < "]
genExpression count
tell ["; "]
genName index
tell ["++"]
genGeneralReplicatorLoop :: A.Name -> A.Expression -> A.Expression -> CGen ()
genGeneralReplicatorLoop index base count
= do counter <- makeNonce "replicator_count" = do counter <- makeNonce "replicator_count"
tell ["int ", counter, " = "] tell ["int ", counter, " = "]
genExpression count genExpression count
tell [", "] tell [", "]
genName n genName index
tell [" = "] tell [" = "]
genExpression base genExpression base
tell ["; ", counter, " > 0; ", counter, "--, "] tell ["; ", counter, " > 0; ", counter, "--, "]
genName n genName index
tell ["++"] tell ["++"]
genReplicatorSize :: A.Replicator -> CGen () genReplicatorSize :: A.Replicator -> CGen ()

View File

@ -46,6 +46,9 @@ works correctly.
## Passes ## Passes
Expression simplification -- this should use generics, so that we can have a
default behaviour that simplifies expressions inside another one.
Output item expressions should be pulled up to variables. Output item expressions should be pulled up to variables.
Before code generation, have a pass that resolves all the DATA TYPE .. IS Before code generation, have a pass that resolves all the DATA TYPE .. IS
@ -64,9 +67,6 @@ Multidimensional array literals won't work.
We could have genSpec generate {} around specs if it's not immediately inside We could have genSpec generate {} around specs if it's not immediately inside
another spec (which'd require some extra boolean arguments to find out). another spec (which'd require some extra boolean arguments to find out).
Replicator loops should be special-cased for when base == 0 to generate the
sort of loop a C programmer would normally write.
genTopLevel should look at what interface the PROC is actually expecting, like genTopLevel should look at what interface the PROC is actually expecting, like
occ21 does. occ21 does.