diff options
| author | Michal Terepeta <michal.terepeta@gmail.com> | 2017-06-23 11:41:50 -0400 | 
|---|---|---|
| committer | Ben Gamari <ben@smart-cactus.org> | 2017-06-23 13:07:30 -0400 | 
| commit | 42eee6eac3d4bf4b2b557cdc13f2d5acae93d4e8 (patch) | |
| tree | 68a7bfe0f71a983784afb6c3ba1fcfdbaf62a546 /compiler/cmm/Hoopl/Label.hs | |
| parent | 9077120918b78f5152bf3596fe6df07b91cead79 (diff) | |
| download | haskell-42eee6eac3d4bf4b2b557cdc13f2d5acae93d4e8.tar.gz | |
Hoopl: remove dependency on Hoopl package
This copies the subset of Hoopl's functionality needed by GHC to
`cmm/Hoopl` and removes the dependency on the Hoopl package.
The main motivation for this change is the confusing/noisy interface
between GHC and Hoopl:
- Hoopl has `Label` which is GHC's `BlockId` but different than
  GHC's `CLabel`
- Hoopl has `Unique` which is different than GHC's `Unique`
- Hoopl has `Unique{Map,Set}` which are different than GHC's
  `Uniq{FM,Set}`
- GHC has its own specialized copy of `Dataflow`, so `cmm/Hoopl` is
  needed just to filter the exposed functions (filter out some of the
  Hoopl's and add the GHC ones)
With this change, we'll be able to simplify this significantly.
It'll also be much easier to do invasive changes (Hoopl is a public
package on Hackage with users that depend on the current behavior)
This should introduce no changes in functionality - it merely
copies the relevant code.
Signed-off-by: Michal Terepeta <michal.terepeta@gmail.com>
Test Plan: ./validate
Reviewers: austin, bgamari, simonmar
Reviewed By: bgamari, simonmar
Subscribers: simonpj, kavon, rwbarton, thomie
Differential Revision: https://phabricator.haskell.org/D3616
Diffstat (limited to 'compiler/cmm/Hoopl/Label.hs')
| -rw-r--r-- | compiler/cmm/Hoopl/Label.hs | 122 | 
1 files changed, 122 insertions, 0 deletions
diff --git a/compiler/cmm/Hoopl/Label.hs b/compiler/cmm/Hoopl/Label.hs new file mode 100644 index 0000000000..5ee4f72fc3 --- /dev/null +++ b/compiler/cmm/Hoopl/Label.hs @@ -0,0 +1,122 @@ +{-# LANGUAGE DeriveFoldable #-} +{-# LANGUAGE DeriveFunctor #-} +{-# LANGUAGE DeriveTraversable #-} +{-# LANGUAGE TypeFamilies #-} +module Hoopl.Label +    ( Label +    , LabelMap +    , LabelSet +    , FactBase +    , lookupFact +    , uniqueToLbl +    ) where + +import Outputable + +import Hoopl.Collections +-- TODO: This should really just use GHC's Unique and Uniq{Set,FM} +import Hoopl.Unique + +import Unique (Uniquable(..)) + +----------------------------------------------------------------------------- +--              Label +----------------------------------------------------------------------------- + +newtype Label = Label { lblToUnique :: Unique } +  deriving (Eq, Ord) + +uniqueToLbl :: Unique -> Label +uniqueToLbl = Label + +instance Show Label where +  show (Label n) = "L" ++ show n + +instance Uniquable Label where +  getUnique label = getUnique (lblToUnique label) + +instance Outputable Label where +  ppr label = ppr (getUnique label) + +----------------------------------------------------------------------------- +-- LabelSet + +newtype LabelSet = LS UniqueSet deriving (Eq, Ord, Show) + +instance IsSet LabelSet where +  type ElemOf LabelSet = Label + +  setNull (LS s) = setNull s +  setSize (LS s) = setSize s +  setMember (Label k) (LS s) = setMember k s + +  setEmpty = LS setEmpty +  setSingleton (Label k) = LS (setSingleton k) +  setInsert (Label k) (LS s) = LS (setInsert k s) +  setDelete (Label k) (LS s) = LS (setDelete k s) + +  setUnion (LS x) (LS y) = LS (setUnion x y) +  setDifference (LS x) (LS y) = LS (setDifference x y) +  setIntersection (LS x) (LS y) = LS (setIntersection x y) +  setIsSubsetOf (LS x) (LS y) = setIsSubsetOf x y + +  setFold k z (LS s) = setFold (k . uniqueToLbl) z s + +  setElems (LS s) = map uniqueToLbl (setElems s) +  setFromList ks = LS (setFromList (map lblToUnique ks)) + +----------------------------------------------------------------------------- +-- LabelMap + +newtype LabelMap v = LM (UniqueMap v) +  deriving (Eq, Ord, Show, Functor, Foldable, Traversable) + +instance IsMap LabelMap where +  type KeyOf LabelMap = Label + +  mapNull (LM m) = mapNull m +  mapSize (LM m) = mapSize m +  mapMember (Label k) (LM m) = mapMember k m +  mapLookup (Label k) (LM m) = mapLookup k m +  mapFindWithDefault def (Label k) (LM m) = mapFindWithDefault def k m + +  mapEmpty = LM mapEmpty +  mapSingleton (Label k) v = LM (mapSingleton k v) +  mapInsert (Label k) v (LM m) = LM (mapInsert k v m) +  mapInsertWith f (Label k) v (LM m) = LM (mapInsertWith f k v m) +  mapDelete (Label k) (LM m) = LM (mapDelete k m) + +  mapUnion (LM x) (LM y) = LM (mapUnion x y) +  mapUnionWithKey f (LM x) (LM y) = LM (mapUnionWithKey (f . uniqueToLbl) x y) +  mapDifference (LM x) (LM y) = LM (mapDifference x y) +  mapIntersection (LM x) (LM y) = LM (mapIntersection x y) +  mapIsSubmapOf (LM x) (LM y) = mapIsSubmapOf x y + +  mapMap f (LM m) = LM (mapMap f m) +  mapMapWithKey f (LM m) = LM (mapMapWithKey (f . uniqueToLbl) m) +  mapFold k z (LM m) = mapFold k z m +  mapFoldWithKey k z (LM m) = mapFoldWithKey (k . uniqueToLbl) z m +  mapFilter f (LM m) = LM (mapFilter f m) + +  mapElems (LM m) = mapElems m +  mapKeys (LM m) = map uniqueToLbl (mapKeys m) +  mapToList (LM m) = [(uniqueToLbl k, v) | (k, v) <- mapToList m] +  mapFromList assocs = LM (mapFromList [(lblToUnique k, v) | (k, v) <- assocs]) +  mapFromListWith f assocs = LM (mapFromListWith f [(lblToUnique k, v) | (k, v) <- assocs]) + +----------------------------------------------------------------------------- +-- Instances + +instance Outputable LabelSet where +  ppr = ppr . setElems + +instance Outputable a => Outputable (LabelMap a) where +  ppr = ppr . mapToList + +----------------------------------------------------------------------------- +-- FactBase + +type FactBase f = LabelMap f + +lookupFact :: Label -> FactBase f -> Maybe f +lookupFact = mapLookup  | 
