CmmLint.hs 7.34 KB
Newer Older
1 2 3
-- The above warning supression flag is a temporary kludge.
-- While working on this module you are encouraged to remove it and fix
-- any warnings in the module. See
Ian Lynagh's avatar
Ian Lynagh committed
4
--     http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
5 6
-- for details

7 8
-----------------------------------------------------------------------------
--
Simon Marlow's avatar
Simon Marlow committed
9
-- (c) The University of Glasgow 2004-2006
10
--
Simon Marlow's avatar
Simon Marlow committed
11
-- CmmLint: checking the correctness of Cmm statements and expressions
12 13 14 15 16 17 18
--
-----------------------------------------------------------------------------

module CmmLint (
  cmmLint, cmmLintTop
  ) where

19
import BlockId
20
import Cmm
Simon Marlow's avatar
Simon Marlow committed
21
import CLabel
22
import Maybe
23 24
import Outputable
import PprCmm
Simon Marlow's avatar
Simon Marlow committed
25 26
import Unique
import Constants
27
import FastString
28

Simon Marlow's avatar
Simon Marlow committed
29
import Control.Monad
30 31 32 33

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

34 35 36
cmmLint :: (Outputable d, Outputable h)
	=> GenCmm d h (ListGraph CmmStmt) -> Maybe SDoc
cmmLint (Cmm tops) = runCmmLint (mapM_ lintCmmTop) tops
37

38 39 40
cmmLintTop :: (Outputable d, Outputable h)
	   => GenCmmTop d h (ListGraph CmmStmt) -> Maybe SDoc
cmmLintTop top = runCmmLint lintCmmTop top
41

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

51
lintCmmTop :: (GenCmmTop h i (ListGraph CmmStmt)) -> CmmLint ()
52
lintCmmTop (CmmProc _ lbl _ (ListGraph blocks))
53
  = addLintInfo (text "in proc " <> pprCLabel lbl) $
54 55 56 57
        let labels = foldl (\s b -> extendBlockSet s (blockId b)) emptyBlockSet blocks
	in  mapM_ (lintCmmBlock labels) blocks

lintCmmTop (CmmData {})
58 59
  = return ()

dias@eecs.harvard.edu's avatar
dias@eecs.harvard.edu committed
60
lintCmmBlock :: BlockSet -> GenBasicBlock CmmStmt -> CmmLint ()
61
lintCmmBlock labels (BasicBlock id stmts)
62
  = addLintInfo (text "in basic block " <> ppr (getUnique id)) $
63
	mapM_ (lintCmmStmt labels) stmts
64 65 66 67 68 69 70

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

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

71
lintCmmExpr :: CmmExpr -> CmmLint CmmType
72 73
lintCmmExpr (CmmLoad expr rep) = do
  lintCmmExpr expr
74
  when (widthInBytes (typeWidth rep) >= wORD_SIZE) $
75 76 77
     cmmCheckWordAddress expr
  return rep
lintCmmExpr expr@(CmmMachOp op args) = do
78 79 80 81
  tys <- mapM lintCmmExpr args
  if map (typeWidth . cmmExprType) args == machOpArgReps op
  	then cmmCheckMachOp op args tys
	else cmmLintMachOpErr expr (map cmmExprType args) (machOpArgReps op)
82
lintCmmExpr (CmmRegOff reg offset)
83
  = lintCmmExpr (CmmMachOp (MO_Add rep)
84
		[CmmReg reg, CmmLit (CmmInt (fromIntegral offset) rep)])
85
  where rep = typeWidth (cmmRegType reg)
86
lintCmmExpr expr = 
87
  return (cmmExprType expr)
88 89

-- Check for some common byte/word mismatches (eg. Sp + 1)
90 91
cmmCheckMachOp   :: MachOp -> [CmmExpr] -> [CmmType] -> CmmLint CmmType
cmmCheckMachOp  op args@[CmmReg reg, CmmLit (CmmInt i _)] _
92 93
  | isWordOffsetReg reg && isOffsetOp op && i `rem` fromIntegral wORD_SIZE /= 0
  = cmmLintDubiousWordOffset (CmmMachOp op args)
94 95 96 97
cmmCheckMachOp op [lit@(CmmLit (CmmInt { })), reg@(CmmReg _)] tys
  = cmmCheckMachOp op [reg, lit] tys
cmmCheckMachOp op _ tys
  = return (machOpResultType op tys)
98

dias@eecs.harvard.edu's avatar
dias@eecs.harvard.edu committed
99
isWordOffsetReg  :: CmmReg -> Bool
100
isWordOffsetReg (CmmGlobal Sp) = True
Simon Marlow's avatar
Simon Marlow committed
101 102
-- No warnings for unaligned arithmetic, which is used to tag dynamic constructor closures.
--isWordOffsetReg (CmmGlobal Hp) = True
103 104
isWordOffsetReg _ = False

dias@eecs.harvard.edu's avatar
dias@eecs.harvard.edu committed
105
isOffsetOp :: MachOp -> Bool
106 107 108 109 110 111
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.
dias@eecs.harvard.edu's avatar
dias@eecs.harvard.edu committed
112
cmmCheckWordAddress :: CmmExpr -> CmmLint ()
113
cmmCheckWordAddress e@(CmmMachOp op [arg, CmmLit (CmmInt i _)])
Simon Marlow's avatar
Simon Marlow committed
114
  | isOffsetOp op && notNodeReg arg && i `rem` fromIntegral wORD_SIZE /= 0
115 116
  = cmmLintDubiousWordOffset e
cmmCheckWordAddress e@(CmmMachOp op [CmmLit (CmmInt i _), arg])
Simon Marlow's avatar
Simon Marlow committed
117
  | isOffsetOp op && notNodeReg arg && i `rem` fromIntegral wORD_SIZE /= 0
118 119 120 121
  = cmmLintDubiousWordOffset e
cmmCheckWordAddress _
  = return ()

Simon Marlow's avatar
Simon Marlow committed
122 123
-- No warnings for unaligned arithmetic with the node register,
-- which is used to extract fields from tagged constructor closures.
dias@eecs.harvard.edu's avatar
dias@eecs.harvard.edu committed
124
notNodeReg :: CmmExpr -> Bool
Simon Marlow's avatar
Simon Marlow committed
125 126
notNodeReg (CmmReg reg) | reg == nodeReg = False
notNodeReg _                             = True
127

128 129 130 131 132 133
lintCmmStmt :: BlockSet -> CmmStmt -> CmmLint ()
lintCmmStmt labels = lint
    where lint (CmmNop) = return ()
          lint (CmmComment {}) = return ()
          lint stmt@(CmmAssign reg expr) = do
            erep <- lintCmmExpr expr
134 135
	    let reg_ty = cmmRegType reg
            if (erep `cmmEqType_ignoring_ptrhood` reg_ty)
136
                then return ()
137
                else cmmLintAssignErr stmt erep reg_ty
138 139 140 141 142
          lint (CmmStore l r) = do
            lintCmmExpr l
            lintCmmExpr r
            return ()
          lint (CmmCall target _res args _ _) =
143
              lintTarget target >> mapM_ (lintCmmExpr . hintlessCmm) args
144 145 146 147
          lint (CmmCondBranch e id) = checkTarget id >> lintCmmExpr e >> checkCond e
          lint (CmmSwitch e branches) = do
            mapM_ checkTarget $ catMaybes branches
            erep <- lintCmmExpr e
148
            if (erep `cmmEqType_ignoring_ptrhood` bWord)
149
              then return ()
150 151 152 153
              else cmmLintErr (text "switch scrutinee is not a word: " <> ppr e <>
                               text " :: " <> ppr erep)
          lint (CmmJump e args) = lintCmmExpr e >> mapM_ (lintCmmExpr . hintlessCmm) args
          lint (CmmReturn ress) = mapM_ (lintCmmExpr . hintlessCmm) ress
154 155 156 157 158 159 160 161
          lint (CmmBranch id)    = checkTarget id
          checkTarget id = if elemBlockSet id labels then return ()
                           else cmmLintErr (text "Branch to nonexistent id" <+> ppr id)

lintTarget :: CmmCallTarget -> CmmLint ()
lintTarget (CmmCallee e _) = lintCmmExpr e >> return ()
lintTarget (CmmPrim {})    = return ()

162

dias@eecs.harvard.edu's avatar
dias@eecs.harvard.edu committed
163
checkCond :: CmmExpr -> CmmLint ()
164 165 166 167
checkCond (CmmMachOp mop _) | isComparisonMachOp mop = return ()
checkCond expr = cmmLintErr (hang (text "expression is not a conditional:") 2
				    (ppr expr))

168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
-- -----------------------------------------------------------------------------
-- CmmLint monad

-- just a basic error monad:

newtype CmmLint a = CmmLint { unCL :: Either SDoc a }

instance Monad CmmLint where
  CmmLint m >>= k = CmmLint $ case m of 
				Left e -> Left e
				Right a -> unCL (k a)
  return a = CmmLint (Right a)

cmmLintErr :: SDoc -> CmmLint a
cmmLintErr msg = CmmLint (Left msg)

addLintInfo :: SDoc -> CmmLint a -> CmmLint a
addLintInfo info thing = CmmLint $ 
   case unCL thing of
	Left err -> Left (hang info 2 err)
	Right a  -> Right a

190
cmmLintMachOpErr :: CmmExpr -> [CmmType] -> [Width] -> CmmLint a
191 192 193 194 195
cmmLintMachOpErr expr argsRep opExpectsRep
     = cmmLintErr (text "in MachOp application: " $$ 
					nest 2 (pprExpr expr) $$
				        (text "op is expecting: " <+> ppr opExpectsRep) $$
					(text "arguments provide: " <+> ppr argsRep))
196

197 198 199 200 201 202 203 204
cmmLintAssignErr :: CmmStmt -> CmmType -> CmmType -> CmmLint a
cmmLintAssignErr stmt e_ty r_ty
  = cmmLintErr (text "in assignment: " $$ 
		nest 2 (vcat [pprStmt stmt, 
			      text "Reg ty:" <+> ppr r_ty,
			      text "Rhs ty:" <+> ppr e_ty]))
			 
					
205 206 207 208 209

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