Debian Codegraph @

src-ghc/Clash/GHC/NetlistTypes.hs

raw
1 {-|
2 Copyright : (C) 2013-2016, University of Twente,
3 2016-2023, Myrtle Software Ltd,
4 2021-2024, QBayLogic B.V.
5 License : BSD2 (see the file LICENSE)
6 Maintainer : QBayLogic B.V. <[email protected]>
7 -}
8
9 {-# LANGUAGE CPP #-}
10 {-# LANGUAGE OverloadedStrings #-}
11 {-# LANGUAGE TemplateHaskell #-}
12
13 module Clash.GHC.NetlistTypes
14 (ghcTypeToHWType)
15 where
16
17 import Data.Coerce (coerce)
18 import Data.Functor.Identity (Identity (..))
19 import Data.Text (pack)
20 import Data.Text.Extra (showt)
21 import Control.Monad.State.Strict (State)
22 import Control.Monad.Trans.Except
23 (Except, ExceptT (..), mapExceptT, runExceptT, throwE)
24 import Control.Monad.Trans.Maybe (MaybeT (..))
25 import Language.Haskell.TH.Syntax (showName)
26
27 import Clash.Core.DataCon (DataCon (..))
28 import Clash.Core.Name (Name (..))
29 import Clash.Core.Pretty (showPpr)
30 import Clash.Core.TyCon (TyConMap, tyConDataCons)
31 import Clash.Core.Type
32 (LitTy (..), Type (..), TypeView (..), coreView, coreView1, tyView)
33 import Clash.Core.Util (tyNatSize, substArgTys)
34 import qualified Clash.Data.UniqMap as UniqMap
35 import Clash.Netlist.Util (coreTypeToHWType, stripFiltered)
36 import Clash.Netlist.Types
37 (HWType(..), HWMap, FilteredHWType(..), PortDirection (..))
38 import Clash.Signal.Internal
39 (ResetPolarity(..), ActiveEdge(..), ResetKind(..)
40 ,InitBehavior(..))
41 import Clash.Util (curLoc)
42
43 import Clash.Annotations.BitRepresentation.Internal
44 (CustomReprs)
45 import Clash.Signal.Internal (KnownDomain)
46
47 ghcTypeToHWType
48 :: Int
49 -- ^ Integer width. The width Clash assumes an Integer to be (instead of it
50 -- begin an arbitrarily large, runtime sized construct).
51 -> CustomReprs
52 -- ^ Custom bit representations
53 -> TyConMap
54 -- ^ Type constructor map
55 -> Type
56 -- ^ Type to convert to HWType
57 -> State HWMap (Maybe (Either String FilteredHWType))
58 ghcTypeToHWType iw = go
59 where
60 -- returnN :: HWType ->
61 returnN t = return (FilteredHWType t [])
62
63 go :: CustomReprs -> TyConMap -> Type -> State HWMap (Maybe (Either String FilteredHWType))
64 go reprs m (AnnType attrs typ) = fmap Just . runExceptT $ do
65 FilteredHWType typ' areVoids <- ExceptT $ coreTypeToHWType go reprs m typ
66 return (FilteredHWType (Annotated attrs typ') areVoids)
67
68 go reprs m ty@(tyView -> TyConApp tc args) = runMaybeT . runExceptT $
69 case nameOcc tc of
70 "GHC.Int.Int8" -> returnN (Signed 8)
71 "GHC.Int.Int16" -> returnN (Signed 16)
72 "GHC.Int.Int32" -> returnN (Signed 32)
73 "GHC.Int.Int64" ->
74 if iw < 64
75 then case tyConDataCons (UniqMap.find tc m) of
76 [dc] -> case dcArgTys dc of
77 [tyView -> TyConApp nm _]
78 | nameOcc nm == "GHC.Prim.Int#" ->
79 throwE $ unlines ["Int64 not supported in forced 32-bit mode on a 64-bit machine."
80 ,"Run Clash with `-fclash-intwidth=64`."
81 ]
82 | nameOcc nm == "GHC.Prim.Int64#" ->
83 returnN (Signed 64)
84 _ -> throwE $ $(curLoc) ++ "Int64 DC has unexpected amount of arguments"
85 _ -> throwE $ $(curLoc) ++ "Int64 TC has unexpected amount of DCs"
86 else returnN (Signed 64)
87 "GHC.Word.Word8" -> returnN (Unsigned 8)
88 "GHC.Word.Word16" -> returnN (Unsigned 16)
89 "GHC.Word.Word32" -> returnN (Unsigned 32)
90 "GHC.Word.Word64" ->
91 if iw < 64
92 then case tyConDataCons (UniqMap.find tc m) of
93 [dc] -> case dcArgTys dc of
94 [tyView -> TyConApp nm _]
95 | nameOcc nm == "GHC.Prim.Word#" ->
96 throwE $ unlines ["Word64 not supported in forced 32-bit mode on a 64-bit machine."
97 ,"Run Clash with `-fclash-intwidth=64`."
98 ]
99 | nameOcc nm == "GHC.Prim.Word64#" ->
100 returnN (Unsigned 64)
101 _ -> throwE $ $(curLoc) ++ "Word64 DC has unexpected amount of arguments"
102 _ -> throwE $ $(curLoc) ++ "Word64 TC has unexpected amount of DCs"
103 else returnN (Unsigned 64)
104 #if MIN_VERSION_ghc(9,0,0)
105 "GHC.Num.Integer.Integer" -> returnN (Signed iw)
106 "GHC.Num.Natural.Natural" -> returnN (Unsigned iw)
107 #else
108 "GHC.Integer.Type.Integer" -> returnN (Signed iw)
109 "GHC.Natural.Natural" -> returnN (Unsigned iw)
110 #endif
111 "GHC.Prim.Char#" -> returnN (Unsigned 21)
112 "GHC.Prim.Int#" -> returnN (Signed iw)
113 "GHC.Prim.Word#" -> returnN (Unsigned iw)
114 "GHC.Prim.Int8#" -> returnN (Signed 8)
115 "GHC.Prim.Int16#" -> returnN (Signed 16)
116 "GHC.Prim.Int32#" -> returnN (Signed 32)
117 "GHC.Prim.Int64#" -> returnN (Signed 64)
118 "GHC.Prim.Word8#" -> returnN (Unsigned 8)
119 "GHC.Prim.Word16#" -> returnN (Unsigned 16)
120 "GHC.Prim.Word32#" -> returnN (Unsigned 32)
121 "GHC.Prim.Word64#" -> returnN (Unsigned 64)
122 "GHC.Prim.Float#" -> returnN (BitVector 32)
123 "GHC.Prim.Double#" -> returnN (BitVector 64)
124 "GHC.Prim.ByteArray#" ->
125 throwE $ "Can't translate type: " ++ showPpr ty
126
127 "GHC.Types.Bool" -> returnN Bool
128 "GHC.Types.Float" -> returnN (BitVector 32)
129 "GHC.Types.Double" -> returnN (BitVector 64)
130 "GHC.Prim.~#" -> returnN (Void Nothing)
131
132 "Clash.Signal.Internal.Signal" ->
133 ExceptT $ MaybeT $ Just <$> coreTypeToHWType go reprs m (args !! 1)
134
135 "Clash.Signal.BiSignal.BiSignalIn" -> do
136 szTy <- case args of
137 [_, _, szTy] -> pure szTy
138 _ -> throwE $ $(curLoc) ++ "BiSignalIn TC has unexpected amount of arguments"
139 let fType ty1 = FilteredHWType ty1 []
140 (fType . BiDirectional In . BitVector . fromInteger) <$>
141 liftE (tyNatSize m szTy)
142
143 "Clash.Signal.BiSignal.BiSignalOut" -> do
144 szTy <- case args of
145 [_, _, szTy] -> pure szTy
146 _ -> throwE $ $(curLoc) ++ "BiSignalOut TC has unexpected amount of arguments"
147 let fType ty1 = FilteredHWType ty1 []
148 (fType . Void . Just . BiDirectional Out . BitVector . fromInteger) <$>
149 liftE (tyNatSize m szTy)
150
151 -- XXX: this is a hack to get a KnownDomain from a KnownConfiguration
152 "GHC.Classes.(%,%)"
153 | [arg0@(tyView -> TyConApp kdNm _), arg1] <- args
154 , nameOcc kdNm == showt ''KnownDomain
155 -> case tyView arg1 of
156 TyConApp kdNm1 _
157 | nameOcc kdNm1 == showt ''KnownDomain
158 -> do k1 <- (stripVoid . stripFiltered) <$> ExceptT (MaybeT (go reprs m arg0))
159 k2 <- (stripVoid . stripFiltered) <$> ExceptT (MaybeT (go reprs m arg1))
160 returnN (Void (Just (Product "GHC.Classes.(%,%)" Nothing [k1,k2])))
161 where
162 stripVoid (Void (Just t)) = t
163 stripVoid t = t
164 _ -> ExceptT (MaybeT (go reprs m arg0))
165
166 -- XXX: this is a hack to get a KnownDomain from a KnownConfiguration
167 "GHC.Classes.CTuple2"
168 | [arg0@(tyView -> TyConApp kdNm _), arg1] <- args
169 , nameOcc kdNm == showt ''KnownDomain
170 -> case tyView arg1 of
171 TyConApp kdNm1 _
172 | nameOcc kdNm1 == showt ''KnownDomain
173 -> do k1 <- (stripVoid . stripFiltered) <$> ExceptT (MaybeT (go reprs m arg0))
174 k2 <- (stripVoid . stripFiltered) <$> ExceptT (MaybeT (go reprs m arg1))
175 returnN (Void (Just (Product "GHC.Classes.CTuple2" Nothing [k1,k2])))
176 where
177 stripVoid (Void (Just t)) = t
178 stripVoid t = t
179 _ -> ExceptT (MaybeT (go reprs m arg0))
180
181 "Clash.Signal.Internal.KnownDomain"
182 -> case tyConDataCons (UniqMap.find tc m) of
183 [dc] -> case substArgTys dc args of
184 [_knownSymbol, _knownNat, tyView -> TyConApp _ [_,dom]] ->
185 case tyView (coreView m dom) of
186 TyConApp _ [tag0, period0, edge0, rstKind0, init0, polarity0] -> do
187 tag1 <- domTag m tag0
188 period1 <- domPeriod m period0
189 edge1 <- domEdge m edge0
190 rstKind1 <- domResetKind m rstKind0
191 init1 <- domInitBehavior m init0
192 polarity1 <- domResetPolarity m polarity0
193 let kd = KnownDomain (pack tag1) period1 edge1 rstKind1 init1 polarity1
194 returnN (Void (Just kd))
195 _ -> ExceptT (MaybeT (pure Nothing))
196 _ -> ExceptT (MaybeT (pure Nothing))
197 _ -> ExceptT (MaybeT (pure Nothing))
198
199 "Clash.Signal.Internal.Clock"
200 | [tag0] <- args
201 -> do
202 tag1 <- domTag m tag0
203 returnN (Clock (pack tag1))
204
205 "Clash.Signal.Internal.ClockN"
206 | [tag0] <- args
207 -> do
208 tag1 <- domTag m tag0
209 returnN (ClockN (pack tag1))
210
211 "Clash.Signal.Internal.Reset"
212 | [tag0] <- args
213 -> do
214 tag1 <- domTag m tag0
215 returnN (Reset (pack tag1))
216
217 "Clash.Signal.Internal.Enable"
218 | [tag0] <- args
219 -> do
220 tag1 <- domTag m tag0
221 returnN (Enable (pack tag1))
222
223 "Clash.Sized.Internal.BitVector.Bit" -> returnN Bit
224
225 "Clash.Sized.Internal.BitVector.BitVector" | n0:_ <- args -> do
226 n <- liftE (tyNatSize m n0)
227 case n of
228 0 -> returnN (Void (Just (BitVector (fromInteger n))))
229 _ -> returnN (BitVector (fromInteger n))
230
231 "Clash.Sized.Internal.Index.Index" | n0:_ <- args -> do
232 n <- liftE (tyNatSize m n0)
233 if n < 2
234 then returnN (Void (Just (Index (fromInteger n))))
235 else returnN (Index (fromInteger n))
236
237 "Clash.Sized.Internal.Signed.Signed" | n0:_ <- args -> do
238 n <- liftE (tyNatSize m n0)
239 if n == 0
240 then returnN (Void (Just (Signed (fromInteger n))))
241 else returnN (Signed (fromInteger n))
242
243 "Clash.Sized.Internal.Unsigned.Unsigned" | n0:_ <- args -> do
244 n <- liftE (tyNatSize m n0)
245 if n == 0
246 then returnN (Void (Just (Unsigned (fromInteger n))))
247 else returnN (Unsigned (fromInteger n))
248
249 "Clash.Sized.Vector.Vec" -> case args of
250 [szTy,elTy] -> do
251 sz0 <- liftE (tyNatSize m szTy)
252 fElHWTy <- ExceptT $ MaybeT $ Just <$> coreTypeToHWType go reprs m elTy
253
254 -- Treat Vec as a product type with a single constructor and N
255 -- constructor fields.
256 let sz1 = fromInteger sz0 :: Int
257 elHWTy = stripFiltered fElHWTy
258
259 let
260 (isVoid, vecHWTy) =
261 case elHWTy of
262 Void {} -> (True, Void (Just (Vector sz1 elHWTy)))
263 _ | sz1 == 0 -> (True, Void (Just (Vector sz1 elHWTy)))
264 _ -> (False, Vector sz1 elHWTy)
265
266 let filtered = [replicate sz1 (isVoid, fElHWTy)]
267 return (FilteredHWType vecHWTy filtered)
268 _ -> throwE $ $(curLoc) ++ "Vec TC has unexpected amount of arguments"
269
270 "Clash.Explicit.BlockRam.Internal.MemBlob" -> case args of
271 [nTy,mTy] -> do
272 n0 <- liftE (tyNatSize m nTy)
273 m0 <- liftE (tyNatSize m mTy)
274 returnN (MemBlob (fromInteger n0) (fromInteger m0))
275 _ -> throwE $ $(curLoc) ++ "MemBlob TC has unexpected amount of arguments"
276
277 "Clash.Sized.RTree.RTree" -> case args of
278 [szTy,elTy] -> do
279 sz0 <- liftE (tyNatSize m szTy)
280 fElHWTy <- ExceptT $ MaybeT $ Just <$> coreTypeToHWType go reprs m elTy
281
282 -- Treat RTree as a product type with a single constructor and 2^N
283 -- constructor fields.
284 let sz1 = fromInteger sz0 :: Int
285 elHWTy = stripFiltered fElHWTy
286
287 let
288 (isVoid, vecHWTy) =
289 case elHWTy of
290 Void {} -> (True, Void (Just (RTree sz1 elHWTy)))
291 _ -> (False, RTree sz1 elHWTy)
292
293 let filtered = [replicate (2^sz1) (isVoid, fElHWTy)]
294 return (FilteredHWType vecHWTy filtered)
295 _ -> throwE $ $(curLoc) ++ "RTree TC has unexpected amount of arguments"
296
297 "String" -> returnN String
298 "GHC.Prim.Addr#" -> returnN String
299 "GHC.Types.[]" | a0:_ <- args -> case tyView a0 of
300 (TyConApp (nameOcc -> "GHC.Types.Char") []) -> returnN String
301 _ -> throwE $ "Can't translate type: " ++ showPpr ty
302 "GHC.Types.List" | a0:_ <- args -> case tyView a0 of
303 (TyConApp (nameOcc -> "GHC.Types.Char") []) -> returnN String
304 _ -> throwE $ "Can't translate type: " ++ showPpr ty
305
306 -- To ensure that Clash doesn't get stuck working away callstacks that
307 -- never end up being used in the generated HDL.
308 "GHC.Stack.Types.CallStack" -> returnN (Void Nothing)
309 "GHC.Internal.Stack.Types.CallStack" -> returnN (Void Nothing)
310
311 "Clash.Explicit.SimIO.SimIO" | a0:_ <- args ->
312 ExceptT $ MaybeT $ Just <$> coreTypeToHWType go reprs m a0
313
314 "Clash.Explicit.SimIO.File" -> returnN FileType
315
316 "Clash.Explicit.SimIO.Reg" -> case args of
317 [aTy] -> ExceptT (MaybeT (Just <$> coreTypeToHWType go reprs m aTy))
318 _ -> throwE $ $(curLoc) ++ "Reg TC has unexpected amount of arguments"
319
320 "GHC.STRef.STRef" -> case args of
321 [_,aTy] -> ExceptT (MaybeT (Just <$> coreTypeToHWType go reprs m aTy))
322 _ -> throwE $ $(curLoc) ++ "STRef TC has unexpected amount of arguments"
323
324 -- Anything that's wrapped in SimOnly should be elided when we generate HDL
325 "Clash.Magic.SimOnly" -> returnN (Void Nothing)
326
327 _ -> ExceptT (MaybeT (pure Nothing))
328
329 go _ _ _ = pure Nothing
330
331 liftE
332 :: Applicative m
333 => Except e a
334 -> ExceptT e (MaybeT m) a
335 liftE = mapExceptT (MaybeT . pure . Just . coerce)
336
337 domTag :: Monad m => TyConMap -> Type -> ExceptT String (MaybeT m) String
338 domTag m (coreView1 m -> Just ty) = domTag m ty
339 domTag _ (LitTy (SymTy tag)) = pure tag
340 domTag _ ty = throwE $ "Internal error. Cannot translate domain tag:\n" ++ showPpr ty
341
342 domPeriod :: Monad m => TyConMap -> Type -> ExceptT String (MaybeT m) Integer
343 domPeriod m (coreView1 m -> Just ty) = domPeriod m ty
344 domPeriod _ (LitTy (NumTy period)) = pure period
345 domPeriod _ ty = throwE $ "Internal error. Cannot translate domain period:\n" ++ showPpr ty
346
347 fromType
348 :: Monad m
349 => String
350 -- ^ Name of type (for error reporting)
351 -> [(String, a)]
352 -- ^ [(Fully qualified constructor name, constructor value)
353 -> TyConMap
354 -- ^ Constructor map (used to look through newtypes)
355 -> Type
356 -- ^ Type representing some constructor
357 -> ExceptT String (MaybeT m) a
358 fromType tyNm constrs m ty =
359 case tyView (coreView m ty) of
360 TyConApp tcNm [] ->
361 go constrs (nameOcc tcNm)
362 _ ->
363 throwE $ "Can't translate " ++ tyNm ++ showPpr ty
364 where
365 go ((cName,c):cs) tcNm =
366 if pack cName == tcNm then
367 pure c
368 else
369 go cs tcNm
370 go [] _ =
371 throwE $ "Can't translate " ++ tyNm ++ showPpr ty
372
373 domEdge
374 :: Monad m
375 => TyConMap
376 -> Type
377 -> ExceptT String (MaybeT m) ActiveEdge
378 domEdge =
379 fromType
380 (showName ''ActiveEdge)
381 [ (showName 'Rising, Rising)
382 , (showName 'Falling, Falling) ]
383
384 domResetKind
385 :: Monad m
386 => TyConMap
387 -> Type
388 -> ExceptT String (MaybeT m) ResetKind
389 domResetKind =
390 fromType
391 (showName ''ResetKind)
392 [ (showName 'Synchronous, Synchronous)
393 , (showName 'Asynchronous, Asynchronous) ]
394
395 domInitBehavior
396 :: Monad m
397 => TyConMap
398 -> Type
399 -> ExceptT String (MaybeT m) InitBehavior
400 domInitBehavior =
401 fromType
402 (showName ''InitBehavior)
403 [ (showName 'Defined, Defined)
404 , (showName 'Unknown, Unknown) ]
405
406 domResetPolarity
407 :: Monad m
408 => TyConMap
409 -> Type
410 -> ExceptT String (MaybeT m) ResetPolarity
411 domResetPolarity =
412 fromType
413 (showName ''ResetPolarity)
414 [ (showName 'ActiveHigh, ActiveHigh)
415 , (showName 'ActiveLow, ActiveLow) ]