CmmLint.hs 7.54 KB
Newer Older
1 2
-----------------------------------------------------------------------------
--
3
-- (c) The University of Glasgow 2011
4
--
Simon Marlow's avatar
Simon Marlow committed
5
-- CmmLint: checking the correctness of Cmm statements and expressions
6 7
--
-----------------------------------------------------------------------------
8
{-# LANGUAGE GADTs #-}
9
module CmmLint (
Simon Marlow's avatar
Simon Marlow committed
10
    cmmLint, cmmLintGraph
11 12
  ) where

13
import Hoopl
14
import Cmm
15 16 17 18
import CmmUtils
import PprCmm ()
import BlockId
import FastString
Simon Marlow's avatar
Simon Marlow committed
19
import Outputable
20
import Constants
21

22
import Data.Maybe
23

24 25 26 27
-- Things to check:
--     - invariant on CmmBlock in CmmExpr (see comment there)
--     - check for branches to blocks that don't exist
--     - check types
28 29 30 31

-- -----------------------------------------------------------------------------
-- Exported entry points:

Ian Lynagh's avatar
Ian Lynagh committed
32
cmmLint :: (Outputable d, Outputable h)
Simon Marlow's avatar
Simon Marlow committed
33
        => GenCmmGroup d h CmmGraph -> Maybe SDoc
34
cmmLint tops = runCmmLint (mapM_ lintCmmDecl) tops
35

36 37
cmmLintGraph :: CmmGraph -> Maybe SDoc
cmmLintGraph g = runCmmLint lintCmmGraph g
38

39 40
runCmmLint :: Outputable a => (a -> CmmLint b) -> a -> Maybe SDoc
runCmmLint l p =
41
   case unCL (l p) of
42 43 44 45 46
     Left err -> Just (vcat [ptext $ sLit ("Cmm lint error:"),
                             nest 2 err,
                             ptext $ sLit ("Program was:"),
                             nest 2 (ppr p)])
     Right _  -> Nothing
47 48 49

lintCmmDecl :: GenCmmDecl h i CmmGraph -> CmmLint ()
lintCmmDecl (CmmProc _ lbl g)
50
  = addLintInfo (text "in proc " <> ppr lbl) $ lintCmmGraph g
51 52 53 54 55 56 57 58 59 60 61 62 63
lintCmmDecl (CmmData {})
  = return ()


lintCmmGraph :: CmmGraph -> CmmLint ()
lintCmmGraph g = mapM_ (lintCmmBlock labels) blocks
  where
       blocks = toBlockList g
       labels = setFromList (map entryLabel blocks)


lintCmmBlock :: BlockSet -> CmmBlock -> CmmLint ()
lintCmmBlock labels block
Simon Marlow's avatar
Simon Marlow committed
64
  = addLintInfo (text "in basic block " <> ppr (entryLabel block)) $ do
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
        let (_, middle, last) = blockSplit block
        mapM_ lintCmmMiddle (blockToList middle)
        lintCmmLast labels last

-- -----------------------------------------------------------------------------
-- lintCmmExpr

-- Checks whether a CmmExpr is "type-correct", and check for obvious-looking
-- byte/word mismatches.

lintCmmExpr :: CmmExpr -> CmmLint CmmType
lintCmmExpr (CmmLoad expr rep) = do
  _ <- lintCmmExpr expr
  -- Disabled, if we have the inlining phase before the lint phase,
  -- we can have funny offsets due to pointer tagging. -- EZY
  -- when (widthInBytes (typeWidth rep) >= wORD_SIZE) $
  --   cmmCheckWordAddress expr
  return rep
lintCmmExpr expr@(CmmMachOp op args) = do
  tys <- mapM lintCmmExpr args
  if map (typeWidth . cmmExprType) args == machOpArgReps op
        then cmmCheckMachOp op args tys
        else cmmLintMachOpErr expr (map cmmExprType args) (machOpArgReps op)
lintCmmExpr (CmmRegOff reg offset)
  = lintCmmExpr (CmmMachOp (MO_Add rep)
                [CmmReg reg, CmmLit (CmmInt (fromIntegral offset) rep)])
  where rep = typeWidth (cmmRegType reg)
lintCmmExpr expr =
  return (cmmExprType expr)

-- Check for some common byte/word mismatches (eg. Sp + 1)
cmmCheckMachOp   :: MachOp -> [CmmExpr] -> [CmmType] -> CmmLint CmmType
cmmCheckMachOp op [lit@(CmmLit (CmmInt { })), reg@(CmmReg _)] tys
  = cmmCheckMachOp op [reg, lit] tys
cmmCheckMachOp op _ tys
  = return (machOpResultType op tys)

isOffsetOp :: MachOp -> Bool
isOffsetOp (MO_Add _) = True
isOffsetOp (MO_Sub _) = True
isOffsetOp _ = False

-- This expression should be an address from which a word can be loaded:
-- check for funny-looking sub-word offsets.
_cmmCheckWordAddress :: CmmExpr -> CmmLint ()
_cmmCheckWordAddress e@(CmmMachOp op [arg, CmmLit (CmmInt i _)])
  | isOffsetOp op && notNodeReg arg && i `rem` fromIntegral wORD_SIZE /= 0
  = cmmLintDubiousWordOffset e
_cmmCheckWordAddress e@(CmmMachOp op [CmmLit (CmmInt i _), arg])
  | isOffsetOp op && notNodeReg arg && i `rem` fromIntegral wORD_SIZE /= 0
  = cmmLintDubiousWordOffset e
_cmmCheckWordAddress _
  = return ()

-- No warnings for unaligned arithmetic with the node register,
-- which is used to extract fields from tagged constructor closures.
notNodeReg :: CmmExpr -> Bool
notNodeReg (CmmReg reg) | reg == nodeReg = False
notNodeReg _                             = True

lintCmmMiddle :: CmmNode O O -> CmmLint ()
lintCmmMiddle node = case node of
  CmmComment _ -> return ()

  CmmAssign reg expr -> do
            erep <- lintCmmExpr expr
            let reg_ty = cmmRegType reg
            if (erep `cmmEqType_ignoring_ptrhood` reg_ty)
                then return ()
                else cmmLintAssignErr (CmmAssign reg expr) erep reg_ty

  CmmStore l r -> do
            _ <- lintCmmExpr l
            _ <- lintCmmExpr r
            return ()

  CmmUnsafeForeignCall target _formals actuals -> do
            lintTarget target
            mapM_ lintCmmExpr actuals


lintCmmLast :: BlockSet -> CmmNode O C -> CmmLint ()
lintCmmLast labels node = case node of
  CmmBranch id -> checkTarget id

  CmmCondBranch e t f -> do
            mapM_ checkTarget [t,f]
            _ <- lintCmmExpr e
            checkCond e

  CmmSwitch e branches -> do
            mapM_ checkTarget $ catMaybes branches
            erep <- lintCmmExpr e
            if (erep `cmmEqType_ignoring_ptrhood` bWord)
              then return ()
160 161
              else cmmLintErr (text "switch scrutinee is not a word: " <>
                               ppr e <> text " :: " <> ppr erep)
Simon Marlow's avatar
Simon Marlow committed
162 163 164 165 166 167 168 169 170 171 172 173

  CmmCall { cml_target = target, cml_cont = cont } -> do
          _ <- lintCmmExpr target
          maybe (return ()) checkTarget cont

  CmmForeignCall tgt _ args succ _ _ -> do
          lintTarget tgt
          mapM_ lintCmmExpr args
          checkTarget succ
 where
  checkTarget id
     | setMember id labels = return ()
Simon Marlow's avatar
Simon Marlow committed
174
     | otherwise = cmmLintErr (text "Branch to nonexistent id" <+> ppr id)
Simon Marlow's avatar
Simon Marlow committed
175 176 177 178 179 180 181 182 183 184 185


lintTarget :: ForeignTarget -> CmmLint ()
lintTarget (ForeignTarget e _) = lintCmmExpr e >> return ()
lintTarget (PrimTarget {})     = return ()


checkCond :: CmmExpr -> CmmLint ()
checkCond (CmmMachOp mop _) | isComparisonMachOp mop = return ()
checkCond (CmmLit (CmmInt x t)) | x == 0 || x == 1, t == wordWidth = return () -- constant values
checkCond expr
186
    = cmmLintErr (hang (text "expression is not a conditional:") 2
187
                         (ppr expr))
188 189 190 191 192 193

-- -----------------------------------------------------------------------------
-- CmmLint monad

-- just a basic error monad:

194
newtype CmmLint a = CmmLint { unCL :: Either SDoc a }
195 196

instance Monad CmmLint where
Simon Marlow's avatar
Simon Marlow committed
197 198 199 200
  CmmLint m >>= k = CmmLint $ case m of
                                Left e -> Left e
                                Right a -> unCL (k a)
  return a = CmmLint (Right a)
201

202
cmmLintErr :: SDoc -> CmmLint a
Simon Marlow's avatar
Simon Marlow committed
203
cmmLintErr msg = CmmLint (Left msg)
204

205
addLintInfo :: SDoc -> CmmLint a -> CmmLint a
Simon Marlow's avatar
Simon Marlow committed
206 207 208
addLintInfo info thing = CmmLint $
   case unCL thing of
        Left err -> Left (hang info 2 err)
209 210 211 212
        Right a  -> Right a

cmmLintMachOpErr :: CmmExpr -> [CmmType] -> [Width] -> CmmLint a
cmmLintMachOpErr expr argsRep opExpectsRep
213 214 215 216
     = cmmLintErr (text "in MachOp application: " $$
                   nest 2 (ppr  expr) $$
                      (text "op is expecting: " <+> ppr opExpectsRep) $$
                      (text "arguments provide: " <+> ppr argsRep))
217 218 219

cmmLintAssignErr :: CmmNode e x -> CmmType -> CmmType -> CmmLint a
cmmLintAssignErr stmt e_ty r_ty
220 221 222 223
  = cmmLintErr (text "in assignment: " $$
                nest 2 (vcat [ppr stmt,
                              text "Reg ty:" <+> ppr r_ty,
                              text "Rhs ty:" <+> ppr e_ty]))
224 225 226 227


cmmLintDubiousWordOffset :: CmmExpr -> CmmLint a
cmmLintDubiousWordOffset expr
228
   = cmmLintErr (text "offset is not a multiple of words: " $$
229
                 nest 2 (ppr expr))