@@ -8,7 +8,7 @@ module Language.Fortran.Analysis.DataFlow
8
8
, genUDMap , genDUMap , duMapToUdMap , UDMap , DUMap
9
9
, genFlowsToGraph , FlowsGraph
10
10
, genVarFlowsToMap , VarFlowsMap
11
- , Constant ( .. ), ParameterVarMap , ConstExpMap , genConstExpMap , analyseConstExps , analyseParameterVars , constantFolding
11
+ , ParameterVarMap , ConstExpMap , genConstExpMap , analyseConstExps , analyseParameterVars
12
12
, genBlockMap , genDefMap , BlockMap , DefMap
13
13
, genCallMap , CallMap
14
14
, loopNodes , genBackEdgeMap , sccWith , BackEdgeMap
@@ -43,12 +43,9 @@ import Data.Maybe
43
43
import Data.List (foldl' , foldl1' , (\\) , union , intersect )
44
44
import Control.Monad.Writer hiding (fix )
45
45
46
- <<<<<<< HEAD
47
- =======
48
- -- import qualified Language.Fortran.Repr as Repr
46
+ import qualified Language.Fortran.Repr as Repr
49
47
import qualified Language.Fortran.Repr.Eval.Value as Repr
50
48
51
- >>>>>>> 63 cef41 (Repr : replace some constant folding)
52
49
--------------------------------------------------
53
50
-- Better names for commonly used types
54
51
type BBNodeMap = IM. IntMap
@@ -360,30 +357,13 @@ maxConst = (2::Integer) ^ (31::Integer) - (1::Integer)
360
357
inBounds :: Integer -> Bool
361
358
inBounds x = minConst <= x && x <= maxConst
362
359
363
- -- | Evaluate possible constant expressions within tree.
364
- constantFolding :: Constant -> Constant
365
- constantFolding c = case c of
366
- ConstBinary binOp a b | ConstInt x <- constantFolding a
367
- , ConstInt y <- constantFolding b -> case binOp of
368
- Addition | inBounds (x + y) -> ConstInt (x + y)
369
- Subtraction | inBounds (x - y) -> ConstInt (x - y)
370
- Multiplication | inBounds (x * y) -> ConstInt (x * y)
371
- Division | y /= 0 -> ConstInt (x `div` y)
372
- -- gfortran appears to do real exponentiation (allowing negative exponent)
373
- -- and cast back to integer via floor() (?) as required
374
- -- but we keep it simple & stick with Haskell-style integer exponentiation
375
- Exponentiation | y >= 0 -> ConstInt (x ^ y)
376
- _ -> ConstBinary binOp (ConstInt x) (ConstInt y)
377
- ConstUnary Minus a | ConstInt x <- constantFolding a -> ConstInt (- x)
378
- ConstUnary Plus a -> constantFolding a
379
- _ -> c
380
-
381
360
-- | The map of all parameter variables and their corresponding values
382
- type ParameterVarMap = M. Map Name Constant
361
+ type ParameterVarMap = M. Map Name Repr. FValue
362
+
383
363
-- | The map of all expressions and whether they are undecided (not
384
- -- present in map), a constant value (Just Constant ), or probably not
385
- -- constant (Nothing).
386
- type ConstExpMap = ASTExprNodeMap (Maybe Constant )
364
+ -- present in map), a constant value (' Just' ), or probably not
365
+ -- constant (' Nothing' ).
366
+ type ConstExpMap = ASTExprNodeMap (Maybe Repr. FValue )
387
367
388
368
-- | Generate a constant-expression map with information about the
389
369
-- expressions (identified by insLabel numbering) in the ProgramFile
@@ -400,23 +380,21 @@ genConstExpMap pf = ceMap
400
380
[ (varName v, getE e)
401
381
| st@ StParameter {} <- universeBi pf :: [Statement (Analysis a )]
402
382
, (Declarator _ _ v ScalarDecl _ (Just e)) <- universeBi st ]
403
- getV :: Expression (Analysis a ) -> Maybe Constant
383
+ getV :: Expression (Analysis a ) -> Maybe Repr. FValue
404
384
getV e = constExp (getAnnotation e) `mplus` (join . flip M. lookup pvMap . varName $ e)
405
385
406
386
-- Generate map of information about 'constant expressions'.
407
387
ceMap = IM. fromList [ (label, doExpr e) | e <- universeBi pf, Just label <- [labelOf e] ]
408
- getE :: Expression (Analysis a ) -> Maybe Constant
388
+ getE :: Expression (Analysis a ) -> Maybe Repr. FValue
409
389
getE = join . (flip IM. lookup ceMap <=< labelOf)
410
390
labelOf = insLabel . getAnnotation
411
- doExpr :: Expression (Analysis a ) -> Maybe Constant
412
- doExpr e = case e of
413
- ExpValue _ _ (ValInteger intStr _) -> Just . ConstInt $ read intStr
414
- ExpValue _ _ (ValReal r _) -> Just $ ConstUninterpReal (prettyHsRealLit r) -- TODO
415
- ExpValue _ _ (ValVariable _) -> getV e
416
- -- Recursively seek information about sub-expressions, relying on laziness.
417
- ExpBinary _ _ binOp e1 e2 -> constantFolding <$> liftM2 (ConstBinary binOp) (getE e1) (getE e2)
418
- ExpUnary _ _ unOp e' -> constantFolding <$> ConstUnary unOp <$> getE e'
419
- _ -> Nothing
391
+ doExpr :: Expression (Analysis a ) -> Maybe Repr. FValue
392
+ doExpr e =
393
+ -- TODO constants may use other constants! but genConstExpMap needs more
394
+ -- changes to support that
395
+ case Repr. runEvalFValuePure mempty (Repr. evalExpr e) of
396
+ Left _err -> Nothing
397
+ Right (a, _msgs) -> Just a
420
398
421
399
-- | Get constant-expression information and put it into the AST
422
400
-- analysis annotation. Must occur after analyseBBlocks.
0 commit comments