From b21f0208623aaae0fc3d358b97a9c4b3f8f103c8 Mon Sep 17 00:00:00 2001 From: Adam Sampson Date: Mon, 17 Mar 2008 18:33:04 +0000 Subject: [PATCH] Make SimplifyExprs compute the type of the variable it declares. This lets you say things like: VAL []INT xs IS [i = 0 FOR 20 | i]: and have it figure out that the type of xs is really [20]INT. This also cleans up the code a very small amount. --- transformations/SimplifyExprs.hs | 39 ++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/transformations/SimplifyExprs.hs b/transformations/SimplifyExprs.hs index a120fa1..ff90e13 100644 --- a/transformations/SimplifyExprs.hs +++ b/transformations/SimplifyExprs.hs @@ -182,20 +182,39 @@ transformConstr = doGeneric `ext1M` doStructured where doGeneric :: Data t => t -> PassM t doGeneric = makeGeneric transformConstr - + + -- This takes a constructor expression: + -- VAL type name IS [i = rep | expr]: + -- ... + -- and produces this: + -- type name: + -- PROCTHEN + -- INT indexvar: + -- SEQ + -- indexvar := 0 + -- SEQ i = rep + -- SEQ + -- name[indexvar] := expr + -- indexvar := indexvar + 1 + -- ... doStructured :: Data a => A.Structured a -> PassM (A.Structured a) - doStructured (A.Spec m (A.Specification m' n (A.IsExpr _ _ t (A.ExprConstr m'' (A.RepConstr _ rep exp)))) scope) - = do indexVarSpec@(A.Specification _ indexVar _) <- makeNonceVariable "array_constr_index" m'' A.Int A.VariableName A.Original + doStructured (A.Spec m (A.Specification m' n (A.IsExpr _ _ _ expr@(A.ExprConstr m'' (A.RepConstr _ rep exp)))) scope) + = do indexVarSpec@(A.Specification _ indexName _) <- makeNonceVariable "array_constr_index" m'' A.Int A.VariableName A.Original + let indexVar = A.Variable m'' indexName scope' <- doGeneric scope + + -- Recompute the type, in order to get the array dimension into it. + t <- typeOfExpression expr + return $ A.Spec m (A.Specification m' n (A.Declaration m' t)) $ A.ProcThen m'' - (A.Seq m'' $ A.Spec m'' (indexVarSpec) $ A.Several m'' [ - A.Only m'' $ A.Assign m'' [A.Variable m'' indexVar] $ A.ExpressionList m'' [A.Literal m'' A.Int $ A.IntLiteral m'' "0"], - A.Rep m'' rep $ A.Only m'' $ A.Seq m'' $ A.Several m'' - [A.Only m'' $ A.Assign m'' - [A.SubscriptedVariable m'' (A.Subscript m'' A.NoCheck $ A.ExprVariable m'' $ A.Variable m'' indexVar) $ A.Variable m'' n] + (A.Seq m'' $ A.Spec m'' indexVarSpec $ A.Several m'' [ + A.Only m'' $ A.Assign m'' [indexVar] $ A.ExpressionList m'' [A.Literal m'' A.Int $ A.IntLiteral m'' "0"], + A.Rep m'' rep $ A.Only m'' $ A.Seq m'' $ A.Several m'' + [ A.Only m'' $ A.Assign m'' + [A.SubscriptedVariable m'' (A.Subscript m'' A.NoCheck $ A.ExprVariable m'' indexVar) $ A.Variable m'' n] $ A.ExpressionList m'' [exp] - ,A.Only m'' $ A.Assign m'' [A.Variable m'' indexVar] $ A.ExpressionList m'' [A.Dyadic m'' A.Plus - (A.ExprVariable m'' $ A.Variable m'' indexVar) + , A.Only m'' $ A.Assign m'' [indexVar] $ A.ExpressionList m'' [A.Dyadic m'' A.Plus + (A.ExprVariable m'' indexVar) (A.Literal m'' A.Int $ A.IntLiteral m'' "1")] ] ])