diff options
author | Joachim Breitner <mail@joachim-breitner.de> | 2018-10-03 15:36:55 +0200 |
---|---|---|
committer | Krzysztof Gogolewski <krz.gogolewski@gmail.com> | 2018-10-03 15:40:22 +0200 |
commit | fc2ff6dd7496a33bf68165b28f37f40b7d647418 (patch) | |
tree | 4cb624e2a8046da6c0bdcb3c6acf0aa1dfd4fa5e /compiler/prelude | |
parent | 3dedffa2a51952b05c22b2d08668155ac07d7320 (diff) | |
download | haskell-fc2ff6dd7496a33bf68165b28f37f40b7d647418.tar.gz |
Make GHC (the library) flexible in the choice of integer library
Summary:
We have more and more users of GHC as a library, for example the
Haskell-to-WebAssembly-compiler https://github.com/tweag/asterius.
These need to make different decisions about various aspects of
code generation than the host compiler, and ideally GHC-the-library
allows them to set the `DynFlags` as needed.
This patch adds a new `DynFlag` that configures which `integer`
library to use. This flag is initialized by `cIntegerLibraryType`
(as before), and is only used in `CorePrep` to decide whether to
use `S#` or not.
The other code paths that were varying based on `cIntegerLibraryType`
are no now longer varying: The trick is to use `integer-wired-in`
as the `-this-unit-id` when compiling either `integer-gmp` or
`integer-simple`.
Test Plan: Validate is happy.
Reviewers: hvr, bgamari
Reviewed By: bgamari
Subscribers: TerrorJack, adamse, simonpj, rwbarton, carter
GHC Trac Issues: #13477
Differential Revision: https://phabricator.haskell.org/D5079
Diffstat (limited to 'compiler/prelude')
-rw-r--r-- | compiler/prelude/PrelNames.hs | 49 |
1 files changed, 35 insertions, 14 deletions
diff --git a/compiler/prelude/PrelNames.hs b/compiler/prelude/PrelNames.hs index d75ad47c6d..d69eaebdcb 100644 --- a/compiler/prelude/PrelNames.hs +++ b/compiler/prelude/PrelNames.hs @@ -110,6 +110,36 @@ by the user. For those things that *can* appear in source programs, original-name cache. See also Note [Built-in syntax and the OrigNameCache] + + +Note [The integer library] +~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Clearly, we need to know the names of various definitions of the integer +library, e.g. the type itself, `mkInteger` etc. But there are two possible +implementations of the integer library: + + * integer-gmp (fast, but uses libgmp, which may not be available on all + targets and is GPL licensed) + * integer-simple (slow, but pure Haskell and BSD-licensed) + +We want the compiler to work with either one. The way we achieve this is: + + * When compiling the integer-{gmp,simple} library, we pass + -this-unit-id integer-wired-in + to GHC (see the cabal file libraries/integer-{gmp,simple}. + * This way, GHC can use just this UnitID (see Module.integerUnitId) when + generating code, and the linker will succeed. + +Unfortuately, the abstraction is not complete: When using integer-gmp, we +really want to use the S# constructor directly. This is controlled by +the `integerLibrary` field of `DynFlags`: If it is IntegerGMP, we use +this constructor directly (see CorePrep.lookupIntegerSDataConName) + +When GHC reads the package data base, it (internally only) pretends it has UnitId +`integer-wired-in` instead of the actual UnitId (which includes the version +number); just like for `base` and other packages, as described in +Note [Wired-in packages] in Module. This is done in Packages.findWiredInPackages. -} {-# LANGUAGE CPP #-} @@ -136,8 +166,6 @@ import Unique import Name import SrcLoc import FastString -import Config ( cIntegerLibraryType, IntegerLibrary(..) ) -import Panic ( panic ) {- ************************************************************************ @@ -355,6 +383,7 @@ basicKnownKeyNames gcdIntegerName, lcmIntegerName, andIntegerName, orIntegerName, xorIntegerName, complementIntegerName, shiftLIntegerName, shiftRIntegerName, bitIntegerName, + integerSDataConName,naturalSDataConName, -- Natural naturalTyConName, @@ -433,9 +462,7 @@ basicKnownKeyNames , typeErrorVAppendDataConName , typeErrorShowTypeDataConName - ] ++ case cIntegerLibraryType of - IntegerGMP -> [integerSDataConName,naturalSDataConName] - IntegerSimple -> [] + ] genericTyConNames :: [Name] genericTyConNames = [ @@ -1118,11 +1145,8 @@ integerTyConName, mkIntegerName, integerSDataConName, gcdIntegerName, lcmIntegerName, andIntegerName, orIntegerName, xorIntegerName, complementIntegerName, shiftLIntegerName, shiftRIntegerName, bitIntegerName :: Name -integerTyConName = tcQual gHC_INTEGER_TYPE (fsLit "Integer") integerTyConKey -integerSDataConName = dcQual gHC_INTEGER_TYPE (fsLit n) integerSDataConKey - where n = case cIntegerLibraryType of - IntegerGMP -> "S#" - IntegerSimple -> panic "integerSDataConName evaluated for integer-simple" +integerTyConName = tcQual gHC_INTEGER_TYPE (fsLit "Integer") integerTyConKey +integerSDataConName = dcQual gHC_INTEGER_TYPE (fsLit "S#") integerSDataConKey mkIntegerName = varQual gHC_INTEGER_TYPE (fsLit "mkInteger") mkIntegerIdKey integerToWord64Name = varQual gHC_INTEGER_TYPE (fsLit "integerToWord64") integerToWord64IdKey integerToInt64Name = varQual gHC_INTEGER_TYPE (fsLit "integerToInt64") integerToInt64IdKey @@ -1169,10 +1193,7 @@ bitIntegerName = varQual gHC_INTEGER_TYPE (fsLit "bitInteger") bit -- GHC.Natural types naturalTyConName, naturalSDataConName :: Name naturalTyConName = tcQual gHC_NATURAL (fsLit "Natural") naturalTyConKey -naturalSDataConName = dcQual gHC_NATURAL (fsLit n) naturalSDataConKey - where n = case cIntegerLibraryType of - IntegerGMP -> "NatS#" - IntegerSimple -> panic "naturalSDataConName evaluated for integer-simple" +naturalSDataConName = dcQual gHC_NATURAL (fsLit "NatS#") naturalSDataConKey naturalFromIntegerName :: Name naturalFromIntegerName = varQual gHC_NATURAL (fsLit "naturalFromInteger") naturalFromIntegerIdKey |