1 -----------------------------------------------------------------------------
3 -- Support code for instrumentation and expansion of the breakpoint combinator
5 -- Pepe Iborra (supported by Google SoC) 2006
7 -----------------------------------------------------------------------------
11 dsAndThenMaybeInsertBreakpoint
12 , maybeInsertBreakpoint
44 import {-#SOURCE#-}DsExpr ( dsLExpr )
47 import Foreign.StablePtr
50 mkBreakpointExpr :: SrcSpan -> Id -> DsM (LHsExpr Id)
51 mkBreakpointExpr loc bkptFuncId = do
54 let mod_name = moduleNameFS$ moduleName mod
55 when (not instrumenting) $
56 warnDs (text "Extracted ids:" <+> (ppr scope $$
57 ppr (map idType scope)))
58 stablePtr <- ioToIOEnv $ newStablePtr scope
59 site <- if instrumenting
60 then recordBkpt (srcSpanStart loc)
62 ATyCon opaqueTyCon <- dsLookupGlobal opaqueTyConName
63 jumpFuncId <- mkJumpFunc bkptFuncId
64 let [opaqueDataCon] = tyConDataCons opaqueTyCon
65 opaqueId = dataConWrapId opaqueDataCon
66 opaqueTy = mkTyConApp opaqueTyCon []
68 l(HsApp (l(HsWrap (WpTyApp (idType id)) (HsVar opaqueId)))
70 -- Yes, I know... I'm gonna burn in hell.
71 Ptr addr# = castStablePtrToPtr stablePtr
72 locals = ExplicitList opaqueTy (map wrapInOpaque scope)
73 locInfo = nlTuple [ HsLit (HsString (packageIdFS$ modulePackageId mod))
74 , HsLit (HsString mod_name)
75 , HsLit (HsInt (fromIntegral site))]
76 funE = l$ HsVar jumpFuncId
77 ptrE = l (HsLit (HsInt (fromIntegral (I# (addr2Int# addr#)))))
80 msgE = l (srcSpanLit loc)
82 l(l(l(l(funE `HsApp` ptrE) `HsApp` locsE) `HsApp` locE) `HsApp` msgE)
84 nlTuple exps = ExplicitTuple (map noLoc exps) Boxed
85 srcSpanLit :: SrcSpan -> HsExpr Id
86 srcSpanLit span = HsLit (HsString (mkFastString (showSDoc (ppr span))))
87 instrumenting = idName bkptFuncId == breakpointAutoName
89 mkBreakpointExpr = undefined -- A stage1 ghc doesn't care about breakpoints
93 getScope = getLocalBindsDs >>= return . filter(isValidType .idType )
94 where isValidType (FunTy a b) = isValidType a && isValidType b
95 isValidType (NoteTy _ t) = isValidType t
96 isValidType (AppTy a b) = isValidType a && isValidType b
97 isValidType (TyConApp con ts) = not (isUnLiftedTyCon con) &&
99 -- isValidType (PredTy p `FunTy` ty ) = False -- TODO: Too restrictive ?
102 dynBreakpoint :: SrcSpan -> DsM (LHsExpr Id)
104 dynBreakpoint loc | not (isGoodSrcSpan loc) =
105 pprPanic "dynBreakpoint: bad SrcSpan" (ppr loc)
107 dynBreakpoint loc = do
108 let autoBreakpoint = Id.mkGlobalId VanillaGlobal breakpointAutoName
109 breakpointAutoTy vanillaIdInfo
111 ioToIOEnv$ debugTraceMsg dflags 3 (text "Breakpoint inserted at " <> ppr loc)
112 return$ L loc (HsVar autoBreakpoint)
113 where breakpointAutoTy = (ForAllTy alphaTyVar
114 (FunTy (TyVarTy alphaTyVar)
115 (TyVarTy alphaTyVar)))
117 -- Records a breakpoint site and returns the site number
118 recordBkpt :: SrcLoc -> DsM (Int)
120 sites_var <- getBkptSitesDs
121 sites <- ioToIOEnv$ readIORef sites_var
122 let site = length sites + 1
123 let coords = (srcLocLine loc, srcLocCol loc)
124 ioToIOEnv$ writeIORef sites_var ((site, coords) : sites)
127 mkJumpFunc :: Id -> DsM Id
128 mkJumpFunc bkptFuncId
129 | idName bkptFuncId == breakpointName
130 = build breakpointJumpName id
131 | idName bkptFuncId == breakpointCondName
132 = build breakpointCondJumpName (FunTy boolTy)
133 | idName bkptFuncId == breakpointAutoName
134 = build breakpointAutoJumpName id
137 basicType extra opaqueTy =
139 (FunTy (mkListTy opaqueTy)
140 (FunTy (mkTupleType [stringTy, stringTy, intTy])
144 (FunTy (TyVarTy tyvar)
145 (TyVarTy tyvar))))))))
146 build name extra = do
147 ATyCon opaqueTyCon <- dsLookupGlobal opaqueTyConName
148 return$ Id.mkGlobalId VanillaGlobal name
149 (basicType extra (mkTyConApp opaqueTyCon [])) vanillaIdInfo
150 mkTupleType tys = mkTupleTy Boxed (length tys) tys
152 debug_enabled, breakpoints_enabled :: DsM Bool
153 dsAndThenMaybeInsertBreakpoint :: LHsExpr Id -> DsM CoreExpr
154 maybeInsertBreakpoint :: LHsExpr Id -> Type -> DsM (LHsExpr Id)
156 #if defined(GHCI) && defined(DEBUGGER)
158 debugging <- doptDs Opt_Debugging
159 b_enabled <- breakpoints_enabled
160 return (debugging && b_enabled)
162 breakpoints_enabled = do
163 ghcMode <- getGhcModeDs
164 currentModule <- getModuleDs
165 ignore_breakpoints <- doptDs Opt_IgnoreBreakpoints
166 return ( not ignore_breakpoints
167 && ghcMode == Interactive
168 && currentModule /= iNTERACTIVE )
170 maybeInsertBreakpoint lhsexpr@(L loc _) ty = do
171 instrumenting <- isInstrumentationSpot lhsexpr
173 if instrumenting && not(isUnLiftedType ty) &&
174 not(isEnabledNullScopeCoalescing && null scope)
175 then do L _ dynBkpt <- dynBreakpoint loc
176 return$ l(HsApp (l$ HsWrap (WpTyApp ty) dynBkpt) lhsexpr)
179 dsAndThenMaybeInsertBreakpoint expr@(L loc _) = do
180 coreExpr <- dsLExpr expr
181 instrumenting <- isInstrumentationSpot expr
183 let ty = exprType coreExpr
184 if instrumenting && not (isUnLiftedType (exprType coreExpr)) &&
185 not(isEnabledNullScopeCoalescing && null scope)
186 then do L _ dynBkpt<- dynBreakpoint loc
187 bkptCore <- dsLExpr (l$ HsWrap (WpTyApp ty) dynBkpt)
188 return (bkptCore `App` coreExpr)
192 maybeInsertBreakpoint expr _ = return expr
193 dsAndThenMaybeInsertBreakpoint coreExpr = dsLExpr coreExpr
194 breakpoints_enabled = return False
195 debug_enabled = return False
199 isInstrumentationSpot (L loc e) = do
200 ghcmode <- getGhcModeDs
201 instrumenting <- debug_enabled
202 return$ instrumenting
203 && isGoodSrcSpan loc -- Avoids 'derived' code
204 && (not$ isRedundant e)
206 isEnabledNullScopeCoalescing = True
207 isRedundant HsLet {} = True
208 isRedundant HsDo {} = True
209 isRedundant HsCase {} = False
210 isRedundant _ = False