Added a pass that helps stop some GCC warnings about literals equivalent to INT_MIN and similar

This commit is contained in:
Neil Brown 2009-01-27 13:00:08 +00:00
parent addcefdab2
commit dbc75ef0b1
2 changed files with 31 additions and 0 deletions

View File

@ -26,6 +26,7 @@ import qualified Data.Map as Map
import qualified AST as A
import CompState
import Errors
import EvalConstants
import Metadata
import Pass
import PrettyShow
@ -43,6 +44,7 @@ squashArrays =
, declareSizesArray
, addSizesFormalParameters
, addSizesActualParameters
, fixMinInt
]
prereq :: [Property]
@ -88,6 +90,32 @@ removeUnneededDirections
_ -> diePC m $ formatCode "Direction applied to non-channel type: %" t
doVariable v = return v
-- | Turns any literals equivalent to a MOSTNEG back into a MOSTNEG
-- The reason for doing this is that C (and presumably C++) don't technically (according
-- to the standard) allow you to write INT_MIN directly as a constant. GCC certainly
-- warns about it. So this pass takes any MOSTNEG-equivalent values (that will have been
-- converted to constants in the constant folding earlier) and turns them back
-- into MOSTNEG, for which the C backend uses INT_MIN and similar, which avoid
-- this problem.
fixMinInt :: Pass
fixMinInt
= cOrCppOnlyPass "Turn any literals that are equal to MOSTNEG INT back into MOSTNEG INT"
prereq
[]
(applyDepthM doExpression)
where
doExpression :: Transform (A.Expression)
doExpression l@(A.Literal m t (A.IntLiteral m' s))
= do folded <- constantFold (A.MostNeg m t)
case folded of
(A.Literal _ _ (A.IntLiteral _ s'), _, _)
-> if (s == s')
then return $ A.MostNeg m t
else return l
_ -> return l -- This can happen as some literals retain the Infer
-- type which fails the constant folding
doExpression e = return e
transformWaitFor :: Pass
transformWaitFor = cOnlyPass "Transform wait for guards into wait until guards"
[]

View File

@ -137,6 +137,9 @@ cOnlyPass = passMakerHelper $ (== BackendC) . csBackend
cppOnlyPass :: PassMaker
cppOnlyPass = passMakerHelper $ (== BackendCPPCSP) . csBackend
cOrCppOnlyPass :: PassMaker
cOrCppOnlyPass = passMakerHelper $ (`elem` [BackendC, BackendCPPCSP]) . csBackend
pass :: PassMaker
pass = passMakerHelper (const True)