summaryrefslogtreecommitdiff
path: root/compiler/deSugar
diff options
context:
space:
mode:
authorDavid Feuer <david.feuer@gmail.com>2017-12-01 15:59:24 -0500
committerDavid Feuer <David.Feuer@gmail.com>2017-12-01 15:59:25 -0500
commit12efb230de40f24e4828734dd46627ebe24416b4 (patch)
tree0c2e501f006d044aed27a0f90757f457082b549b /compiler/deSugar
parente1fb28384c44fcd29b0e60b9fd44767be22646af (diff)
downloadhaskell-12efb230de40f24e4828734dd46627ebe24416b4.tar.gz
Add trace injection
Add support for injecting runtime calls to `trace` in `DsM`. This allows the desugarer to add compile-time information to a runtime trace. Reviewers: austin, hvr, bgamari Reviewed By: bgamari Subscribers: carter, thomie, rwbarton Differential Revision: https://phabricator.haskell.org/D4162
Diffstat (limited to 'compiler/deSugar')
-rw-r--r--compiler/deSugar/DsMonad.hs34
1 files changed, 33 insertions, 1 deletions
diff --git a/compiler/deSugar/DsMonad.hs b/compiler/deSugar/DsMonad.hs
index 1eabf02161..ae39e3de5a 100644
--- a/compiler/deSugar/DsMonad.hs
+++ b/compiler/deSugar/DsMonad.hs
@@ -49,7 +49,10 @@ module DsMonad (
CanItFail(..), orFail,
-- Levity polymorphism
- dsNoLevPoly, dsNoLevPolyExpr, dsWhenNoErrs
+ dsNoLevPoly, dsNoLevPolyExpr, dsWhenNoErrs,
+
+ -- Trace injection
+ pprRuntimeTrace
) where
import GhcPrelude
@@ -87,6 +90,7 @@ import Maybes
import Var (EvVar)
import qualified GHC.LanguageExtensions as LangExt
import UniqFM ( lookupWithDefaultUFM )
+import Literal ( mkMachString )
import Data.IORef
import Control.Monad
@@ -732,3 +736,31 @@ dsLookupDPHRdrEnv_maybe occ
_ -> pprPanic multipleNames (ppr occ)
}
where multipleNames = "Multiple definitions in 'Data.Array.Parallel' and 'Data.Array.Parallel.Prim':"
+
+-- | Inject a trace message into the compiled program. Whereas
+-- pprTrace prints out information *while compiling*, pprRuntimeTrace
+-- captures that information and causes it to be printed *at runtime*
+-- using Debug.Trace.trace.
+--
+-- pprRuntimeTrace hdr doc expr
+--
+-- will produce an expression that looks like
+--
+-- trace (hdr + doc) expr
+--
+-- When using this to debug a module that Debug.Trace depends on,
+-- it is necessary to import {-# SOURCE #-} Debug.Trace () in that
+-- module. We could avoid this inconvenience by wiring in Debug.Trace.trace,
+-- but that doesn't seem worth the effort and maintenance cost.
+pprRuntimeTrace :: String -- ^ header
+ -> SDoc -- ^ information to output
+ -> CoreExpr -- ^ expression
+ -> DsM CoreExpr
+pprRuntimeTrace str doc expr = do
+ traceId <- dsLookupGlobalId traceName
+ unpackCStringId <- dsLookupGlobalId unpackCStringName
+ dflags <- getDynFlags
+ let message :: CoreExpr
+ message = App (Var unpackCStringId) $
+ Lit $ mkMachString $ showSDoc dflags (hang (text str) 4 doc)
+ return $ mkApps (Var traceId) [Type (exprType expr), message, expr]