1 -- The above warning supression flag is a temporary kludge.
2 -- While working on this module you are encouraged to remove it and fix
3 -- any warnings in the module. See
4 -- http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
7 -----------------------------------------------------------------------------
9 -- (c) The University of Glasgow 2004-2006
11 -- CmmLint: checking the correctness of Cmm statements and expressions
13 -----------------------------------------------------------------------------
29 -- -----------------------------------------------------------------------------
30 -- Exported entry points:
32 cmmLint :: (Outputable d, Outputable h)
33 => GenCmm d h (ListGraph CmmStmt) -> Maybe SDoc
34 cmmLint (Cmm tops) = runCmmLint (mapM_ lintCmmTop) tops
36 cmmLintTop :: (Outputable d, Outputable h)
37 => GenCmmTop d h (ListGraph CmmStmt) -> Maybe SDoc
38 cmmLintTop top = runCmmLint lintCmmTop top
40 runCmmLint :: Outputable a => (a -> CmmLint b) -> a -> Maybe SDoc
43 Left err -> Just (vcat [ptext $ sLit ("Cmm lint error:"),
45 ptext $ sLit ("Program was:"),
49 lintCmmTop :: (GenCmmTop h i (ListGraph CmmStmt)) -> CmmLint ()
50 lintCmmTop (CmmProc _ lbl (ListGraph blocks))
51 = addLintInfo (text "in proc " <> pprCLabel lbl) $
52 let labels = foldl (\s b -> setInsert (blockId b) s) setEmpty blocks
53 in mapM_ (lintCmmBlock labels) blocks
55 lintCmmTop (CmmData {})
58 lintCmmBlock :: BlockSet -> GenBasicBlock CmmStmt -> CmmLint ()
59 lintCmmBlock labels (BasicBlock id stmts)
60 = addLintInfo (text "in basic block " <> ppr id) $
61 mapM_ (lintCmmStmt labels) stmts
63 -- -----------------------------------------------------------------------------
66 -- Checks whether a CmmExpr is "type-correct", and check for obvious-looking
67 -- byte/word mismatches.
69 lintCmmExpr :: CmmExpr -> CmmLint CmmType
70 lintCmmExpr (CmmLoad expr rep) = do
72 -- Disabled, if we have the inlining phase before the lint phase,
73 -- we can have funny offsets due to pointer tagging. -- EZY
74 -- when (widthInBytes (typeWidth rep) >= wORD_SIZE) $
75 -- cmmCheckWordAddress expr
77 lintCmmExpr expr@(CmmMachOp op args) = do
78 tys <- mapM lintCmmExpr args
79 if map (typeWidth . cmmExprType) args == machOpArgReps op
80 then cmmCheckMachOp op args tys
81 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)
87 return (cmmExprType expr)
89 -- Check for some common byte/word mismatches (eg. Sp + 1)
90 cmmCheckMachOp :: MachOp -> [CmmExpr] -> [CmmType] -> CmmLint CmmType
91 cmmCheckMachOp op [lit@(CmmLit (CmmInt { })), reg@(CmmReg _)] tys
92 = cmmCheckMachOp op [reg, lit] tys
93 cmmCheckMachOp op _ tys
94 = return (machOpResultType op tys)
96 isOffsetOp :: MachOp -> Bool
97 isOffsetOp (MO_Add _) = True
98 isOffsetOp (MO_Sub _) = True
101 -- This expression should be an address from which a word can be loaded:
102 -- check for funny-looking sub-word offsets.
103 _cmmCheckWordAddress :: CmmExpr -> CmmLint ()
104 _cmmCheckWordAddress e@(CmmMachOp op [arg, CmmLit (CmmInt i _)])
105 | isOffsetOp op && notNodeReg arg && i `rem` fromIntegral wORD_SIZE /= 0
106 = cmmLintDubiousWordOffset e
107 _cmmCheckWordAddress e@(CmmMachOp op [CmmLit (CmmInt i _), arg])
108 | isOffsetOp op && notNodeReg arg && i `rem` fromIntegral wORD_SIZE /= 0
109 = cmmLintDubiousWordOffset e
110 _cmmCheckWordAddress _
113 -- No warnings for unaligned arithmetic with the node register,
114 -- which is used to extract fields from tagged constructor closures.
115 notNodeReg :: CmmExpr -> Bool
116 notNodeReg (CmmReg reg) | reg == nodeReg = False
119 lintCmmStmt :: BlockSet -> CmmStmt -> CmmLint ()
120 lintCmmStmt labels = lint
121 where lint (CmmNop) = return ()
122 lint (CmmComment {}) = return ()
123 lint stmt@(CmmAssign reg expr) = do
124 erep <- lintCmmExpr expr
125 let reg_ty = cmmRegType reg
126 if (erep `cmmEqType_ignoring_ptrhood` reg_ty)
128 else cmmLintAssignErr stmt erep reg_ty
129 lint (CmmStore l r) = do
133 lint (CmmCall target _res args _ _) =
134 lintTarget target >> mapM_ (lintCmmExpr . hintlessCmm) args
135 lint (CmmCondBranch e id) = checkTarget id >> lintCmmExpr e >> checkCond e
136 lint (CmmSwitch e branches) = do
137 mapM_ checkTarget $ catMaybes branches
138 erep <- lintCmmExpr e
139 if (erep `cmmEqType_ignoring_ptrhood` bWord)
141 else cmmLintErr (text "switch scrutinee is not a word: " <> ppr e <>
142 text " :: " <> ppr erep)
143 lint (CmmJump e args) = lintCmmExpr e >> mapM_ (lintCmmExpr . hintlessCmm) args
144 lint (CmmReturn ress) = mapM_ (lintCmmExpr . hintlessCmm) ress
145 lint (CmmBranch id) = checkTarget id
146 checkTarget id = if setMember id labels then return ()
147 else cmmLintErr (text "Branch to nonexistent id" <+> ppr id)
149 lintTarget :: CmmCallTarget -> CmmLint ()
150 lintTarget (CmmCallee e _) = lintCmmExpr e >> return ()
151 lintTarget (CmmPrim {}) = return ()
154 checkCond :: CmmExpr -> CmmLint ()
155 checkCond (CmmMachOp mop _) | isComparisonMachOp mop = return ()
156 checkCond expr = cmmLintErr (hang (text "expression is not a conditional:") 2
159 -- -----------------------------------------------------------------------------
162 -- just a basic error monad:
164 newtype CmmLint a = CmmLint { unCL :: Either SDoc a }
166 instance Monad CmmLint where
167 CmmLint m >>= k = CmmLint $ case m of
169 Right a -> unCL (k a)
170 return a = CmmLint (Right a)
172 cmmLintErr :: SDoc -> CmmLint a
173 cmmLintErr msg = CmmLint (Left msg)
175 addLintInfo :: SDoc -> CmmLint a -> CmmLint a
176 addLintInfo info thing = CmmLint $
178 Left err -> Left (hang info 2 err)
181 cmmLintMachOpErr :: CmmExpr -> [CmmType] -> [Width] -> CmmLint a
182 cmmLintMachOpErr expr argsRep opExpectsRep
183 = cmmLintErr (text "in MachOp application: " $$
185 (text "op is expecting: " <+> ppr opExpectsRep) $$
186 (text "arguments provide: " <+> ppr argsRep))
188 cmmLintAssignErr :: CmmStmt -> CmmType -> CmmType -> CmmLint a
189 cmmLintAssignErr stmt e_ty r_ty
190 = cmmLintErr (text "in assignment: " $$
191 nest 2 (vcat [ppr stmt,
192 text "Reg ty:" <+> ppr r_ty,
193 text "Rhs ty:" <+> ppr e_ty]))
197 cmmLintDubiousWordOffset :: CmmExpr -> CmmLint a
198 cmmLintDubiousWordOffset expr
199 = cmmLintErr (text "offset is not a multiple of words: " $$