CmmLint.hs 7.17 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 MachOp
23
import Maybe
24 25
import Outputable
import PprCmm
Simon Marlow's avatar
Simon Marlow committed
26 27
import Unique
import Constants
28
import FastString
29

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

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

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

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

runCmmLint :: CmmLint a -> Maybe SDoc
runCmmLint l = 
   case unCL l of
Ian Lynagh's avatar
Ian Lynagh committed
44
	Left err -> Just (ptext (sLit "Cmm lint error:") $$ nest 2 err)
45 46
	Right _  -> Nothing

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

lintCmmTop (CmmData {})
54 55
  = return ()

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

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

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

lintCmmExpr :: CmmExpr -> CmmLint MachRep
lintCmmExpr (CmmLoad expr rep) = do
  lintCmmExpr expr
  when (machRepByteWidth rep >= wORD_SIZE) $
     cmmCheckWordAddress expr
  return rep
lintCmmExpr expr@(CmmMachOp op args) = do
  mapM_ lintCmmExpr args
  if map cmmExprRep args == machOpArgReps op
  	then cmmCheckMachOp op args
77
	else cmmLintMachOpErr expr (map cmmExprRep args) (machOpArgReps op)
78 79 80 81
lintCmmExpr (CmmRegOff reg offset)
  = lintCmmExpr (CmmMachOp (MO_Add rep) 
		[CmmReg reg, CmmLit (CmmInt (fromIntegral offset) rep)])
  where rep = cmmRegRep reg
82 83 84
lintCmmExpr lit@(CmmLit (CmmInt _ rep))
  | isFloatingRep rep
  = cmmLintErr (text "integer literal with floating MachRep: " <> ppr lit)
85 86 87 88
lintCmmExpr expr = 
  return (cmmExprRep expr)

-- Check for some common byte/word mismatches (eg. Sp + 1)
dias@eecs.harvard.edu's avatar
dias@eecs.harvard.edu committed
89
cmmCheckMachOp   :: MachOp -> [CmmExpr] -> CmmLint MachRep
90 91 92
cmmCheckMachOp  op args@[CmmReg reg, CmmLit (CmmInt i _)]
  | isWordOffsetReg reg && isOffsetOp op && i `rem` fromIntegral wORD_SIZE /= 0
  = cmmLintDubiousWordOffset (CmmMachOp op args)
93
cmmCheckMachOp op [lit@(CmmLit (CmmInt { })), reg@(CmmReg _)]
94
  = cmmCheckMachOp op [reg, lit]
95 96 97 98
cmmCheckMachOp op@(MO_U_Conv from to) args
  | isFloatingRep from || isFloatingRep to
  = cmmLintErr (text "unsigned conversion from/to floating rep: " 
		<> ppr (CmmMachOp op args))
99
cmmCheckMachOp op _args
100 101
  = return (resultRepOfMachOp op)

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

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

Simon Marlow's avatar
Simon Marlow committed
125 126
-- 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
127
notNodeReg :: CmmExpr -> Bool
Simon Marlow's avatar
Simon Marlow committed
128 129
notNodeReg (CmmReg reg) | reg == nodeReg = False
notNodeReg _                             = True
130

131 132 133 134 135 136 137 138 139 140 141 142 143 144
lintCmmStmt :: BlockSet -> CmmStmt -> CmmLint ()
lintCmmStmt labels = lint
    where lint (CmmNop) = return ()
          lint (CmmComment {}) = return ()
          lint stmt@(CmmAssign reg expr) = do
            erep <- lintCmmExpr expr
            if (erep == cmmRegRep reg)
                then return ()
                else cmmLintAssignErr stmt
          lint (CmmStore l r) = do
            lintCmmExpr l
            lintCmmExpr r
            return ()
          lint (CmmCall target _res args _ _) =
145
              lintTarget target >> mapM_ (lintCmmExpr . kindlessCmm) args
146 147 148 149 150 151 152
          lint (CmmCondBranch e id) = checkTarget id >> lintCmmExpr e >> checkCond e
          lint (CmmSwitch e branches) = do
            mapM_ checkTarget $ catMaybes branches
            erep <- lintCmmExpr e
            if (erep == wordRep)
              then return ()
              else cmmLintErr (text "switch scrutinee is not a word: " <> ppr e)
153 154
          lint (CmmJump e args) = lintCmmExpr e >> mapM_ (lintCmmExpr . kindlessCmm) args
          lint (CmmReturn ress) = mapM_ (lintCmmExpr . kindlessCmm) ress
155 156 157 158 159 160 161 162
          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 ()

163

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

169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
-- -----------------------------------------------------------------------------
-- 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

191 192 193 194 195 196
cmmLintMachOpErr :: CmmExpr -> [MachRep] -> [MachRep] -> CmmLint a
cmmLintMachOpErr expr argsRep opExpectsRep
     = cmmLintErr (text "in MachOp application: " $$ 
					nest 2 (pprExpr expr) $$
				        (text "op is expecting: " <+> ppr opExpectsRep) $$
					(text "arguments provide: " <+> ppr argsRep))
197 198 199 200 201 202 203 204 205

cmmLintAssignErr :: CmmStmt -> CmmLint a
cmmLintAssignErr stmt = cmmLintErr (text "in assignment: " $$ 
					nest 2 (pprStmt stmt))

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