Skip to content

Commit 4186a40

Browse files
committed
Allow GHC to dump primop modules
This patch adds two commands to GHC: - --print-prim-module: prints the contents of GHC.Internal.Prim - --print-prim-wrappers-module: prints the contents of GHC.Internal.PrimopWrappers These two commands can be used in ghc-internal's Setup.hs to generate these modules. This ensures that ghc-internal's primop code is always in sync with the GHC that builds it. It also avoids having to share code (primops.txt.pp and friends) between ghc-internal and ghc.
1 parent ada0403 commit 4186a40

File tree

6 files changed

+41
-3
lines changed

6 files changed

+41
-3
lines changed

compiler/GHC/Builtin/PrimOps.hs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ module GHC.Builtin.PrimOps (
2525

2626
getPrimOpResultInfo, isComparisonPrimOp, PrimOpResultInfo(..),
2727

28-
PrimCall(..)
28+
PrimCall(..),
29+
30+
primOpPrimModule, primOpWrappersModule
2931
) where
3032

3133
import GHC.Prelude
@@ -171,6 +173,12 @@ primOpDocs :: [(FastString, String)]
171173
primOpDeprecations :: [(OccName, FastString)]
172174
#include "primop-deprecations.hs-incl"
173175

176+
primOpPrimModule :: String
177+
#include "primop-prim-module.hs-incl"
178+
179+
primOpWrappersModule :: String
180+
#include "primop-wrappers-module.hs-incl"
181+
174182
{-
175183
************************************************************************
176184
* *

compiler/Setup.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ primopIncls =
5353
, ("primop-vector-tycons.hs-incl" , "--primop-vector-tycons")
5454
, ("primop-docs.hs-incl" , "--wired-in-docs")
5555
, ("primop-deprecations.hs-incl" , "--wired-in-deprecations")
56+
, ("primop-prim-module.hs-incl" , "--prim-module")
57+
, ("primop-wrappers-module.hs-incl" , "--wrappers-module")
5658
]
5759

5860
ghcAutogen :: Verbosity -> LocalBuildInfo -> IO ()

ghc/Main.hs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ import GHC.Platform
3838
import GHC.Platform.Ways
3939
import GHC.Platform.Host
4040

41+
import GHC.Builtin.PrimOps (primOpPrimModule, primOpWrappersModule)
42+
4143
#if defined(HAVE_INTERNAL_INTERPRETER)
4244
import GHCi.UI ( interactiveUI, ghciWelcomeMsg, defaultGhciSettings )
4345
#endif
@@ -147,6 +149,8 @@ main = do
147149
ShowVersion -> showVersion
148150
ShowNumVersion -> putStrLn cProjectVersion
149151
ShowOptions isInteractive -> showOptions isInteractive
152+
PrintPrimModule -> liftIO $ putStrLn primOpPrimModule
153+
PrintPrimWrappersModule -> liftIO $ putStrLn primOpWrappersModule
150154
Right postStartupMode ->
151155
-- start our GHC session
152156
GHC.runGhc mbMinusB $ do
@@ -451,12 +455,16 @@ data PreStartupMode
451455
| ShowNumVersion -- ghc --numeric-version
452456
| ShowSupportedExtensions -- ghc --supported-extensions
453457
| ShowOptions Bool {- isInteractive -} -- ghc --show-options
458+
| PrintPrimModule -- ghc --print-prim-module
459+
| PrintPrimWrappersModule -- ghc --print-prim-wrappers-module
454460

455-
showVersionMode, showNumVersionMode, showSupportedExtensionsMode, showOptionsMode :: Mode
461+
showVersionMode, showNumVersionMode, showSupportedExtensionsMode, showOptionsMode, printPrimModule, printPrimWrappersModule :: Mode
456462
showVersionMode = mkPreStartupMode ShowVersion
457463
showNumVersionMode = mkPreStartupMode ShowNumVersion
458464
showSupportedExtensionsMode = mkPreStartupMode ShowSupportedExtensions
459465
showOptionsMode = mkPreStartupMode (ShowOptions False)
466+
printPrimModule = mkPreStartupMode PrintPrimModule
467+
printPrimWrappersModule = mkPreStartupMode PrintPrimWrappersModule
460468

461469
mkPreStartupMode :: PreStartupMode -> Mode
462470
mkPreStartupMode = Left
@@ -622,6 +630,8 @@ mode_flags =
622630
, defFlag "-numeric-version" (PassFlag (setMode showNumVersionMode))
623631
, defFlag "-info" (PassFlag (setMode showInfoMode))
624632
, defFlag "-show-options" (PassFlag (setMode showOptionsMode))
633+
, defFlag "-print-prim-module" (PassFlag (setMode printPrimModule))
634+
, defFlag "-print-prim-wrappers-module" (PassFlag (setMode printPrimWrappersModule))
625635
, defFlag "-supported-languages" (PassFlag (setMode showSupportedExtensionsMode))
626636
, defFlag "-supported-extensions" (PassFlag (setMode showSupportedExtensionsMode))
627637
, defFlag "-show-packages" (PassFlag (setMode showUnitsMode))

hadrian/src/Rules/Generate.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ compilerDependencies = do
101101
, "primop-vector-uniques.hs-incl"
102102
, "primop-docs.hs-incl"
103103
, "primop-deprecations.hs-incl"
104+
, "primop-prim-module.hs-incl"
105+
, "primop-wrappers-module.hs-incl"
104106
, "GHC/Platform/Constants.hs"
105107
, "GHC/Settings/Config.hs"
106108
]

hadrian/src/Settings/Builders/GenPrimopCode.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,6 @@ genPrimopCodeBuilderArgs = builder GenPrimopCode ? mconcat
2424
, output "//primop-vector-tycons.hs-incl" ? arg "--primop-vector-tycons"
2525
, output "//primop-docs.hs-incl" ? arg "--wired-in-docs"
2626
, output "//primop-deprecations.hs-incl" ? arg "--wired-in-deprecations"
27+
, output "//primop-prim-module.hs-incl" ? arg "--prim-module"
28+
, output "//primop-wrappers-module.hs-incl" ? arg "--wrappers-module"
2729
, output "//primop-usage.hs-incl" ? arg "--usage" ]

utils/genprimopcode/Main.hs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,12 @@ main = getArgs >>= \args ->
198198
"--make-haskell-source"
199199
-> putStr (gen_hs_source p_o_specs)
200200

201+
"--wrappers-module"
202+
-> putStr (gen_wrappers_module p_o_specs)
203+
204+
"--prim-module"
205+
-> putStr (gen_hs_source_module p_o_specs)
206+
201207
"--wired-in-docs"
202208
-> putStr (gen_wired_in_docs p_o_specs)
203209

@@ -229,13 +235,18 @@ known_args
229235
"--make-haskell-source",
230236
"--make-latex-doc",
231237
"--wired-in-docs",
232-
"--wired-in-deprecations"
238+
"--wired-in-deprecations",
239+
"--wrappers-module",
240+
"--prim-module"
233241
]
234242

235243
------------------------------------------------------------------
236244
-- Code generators -----------------------------------------------
237245
------------------------------------------------------------------
238246

247+
gen_hs_source_module :: Info -> String
248+
gen_hs_source_module info = "primOpPrimModule = " ++ show (gen_hs_source info)
249+
239250
gen_hs_source :: Info -> String
240251
gen_hs_source (Info defaults entries) =
241252
"{-\n"
@@ -461,6 +472,9 @@ In PrimopWrappers we set some crucial GHC options
461472
a very simple module and there is no optimisation to be done
462473
-}
463474

475+
gen_wrappers_module :: Info -> String
476+
gen_wrappers_module info = "primOpWrappersModule = " ++ show (gen_wrappers info)
477+
464478
gen_wrappers :: Info -> String
465479
gen_wrappers (Info _ entries)
466480
= "-- | Users should not import this module. It is GHC internal only.\n"

0 commit comments

Comments
 (0)