From 7d2013d3f14aa3ab57ed4d3bfb005533fd4cbbed Mon Sep 17 00:00:00 2001 From: Adam Sampson Date: Tue, 24 Apr 2007 13:03:08 +0000 Subject: [PATCH] Generate more natural replicator loops when base == 0 --- fco2/GenerateC.hs | 28 +++++++++++++++++++++++++--- fco2/TODO | 6 +++--- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/fco2/GenerateC.hs b/fco2/GenerateC.hs index 33ca75d..ad99778 100644 --- a/fco2/GenerateC.hs +++ b/fco2/GenerateC.hs @@ -544,17 +544,39 @@ genReplicator rep body body 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.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" tell ["int ", counter, " = "] genExpression count tell [", "] - genName n + genName index tell [" = "] genExpression base tell ["; ", counter, " > 0; ", counter, "--, "] - genName n + genName index tell ["++"] genReplicatorSize :: A.Replicator -> CGen () diff --git a/fco2/TODO b/fco2/TODO index 4e859d7..b016711 100644 --- a/fco2/TODO +++ b/fco2/TODO @@ -46,6 +46,9 @@ works correctly. ## 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. 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 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 occ21 does.