1 module Main(main) where
4 import qualified CoreUtils
8 import qualified DataCon
10 import qualified Module
11 import qualified Control.Monad.State as State
14 import NameEnv ( lookupNameEnv )
15 import HscTypes ( cm_binds, cm_types )
16 import MonadUtils ( liftIO )
17 import Outputable ( showSDoc, ppr )
18 import GHC.Paths ( libdir )
19 import DynFlags ( defaultDynFlags )
22 import qualified Monad
24 -- The following modules come from the ForSyDe project. They are really
25 -- internal modules, so ForSyDe.cabal has to be modified prior to installing
26 -- ForSyDe to get access to these modules.
27 import qualified ForSyDe.Backend.VHDL.AST as AST
28 import qualified ForSyDe.Backend.VHDL.Ppr
29 import qualified ForSyDe.Backend.Ppr
30 -- This is needed for rendering the pretty printed VHDL
31 import Text.PrettyPrint.HughesPJ (render)
35 defaultErrorHandler defaultDynFlags $ do
36 runGhc (Just libdir) $ do
37 dflags <- getSessionDynFlags
38 setSessionDynFlags dflags
39 --target <- guessTarget "adder.hs" Nothing
40 --liftIO (print (showSDoc (ppr (target))))
41 --liftIO $ printTarget target
44 --core <- GHC.compileToCoreSimplified "Adders.hs"
45 core <- GHC.compileToCoreSimplified "Adders.hs"
46 --liftIO $ printBinds (cm_binds core)
47 let binds = Maybe.mapMaybe (findBind (cm_binds core)) ["full_adder", "half_adder"]
48 liftIO $ printBinds binds
49 -- Turn bind into VHDL
50 let vhdl = State.evalState (mkVHDL binds) (VHDLSession 0 [])
51 liftIO $ putStr $ render $ ForSyDe.Backend.Ppr.ppr vhdl
54 -- Turns the given bind into VHDL
56 -- Add the builtin functions
57 mapM (uncurry addFunc) builtin_funcs
58 -- Get the function signatures
59 funcs <- mapM mkHWFunction binds
60 -- Add them to the session
61 mapM (uncurry addFunc) funcs
62 let entities = map getEntity (snd $ unzip funcs)
63 -- Create architectures for them
64 archs <- mapM getArchitecture binds
65 return $ AST.DesignFile
67 ((map AST.LUEntity entities) ++ (map AST.LUArch archs))
69 printTarget (Target (TargetFile file (Just x)) obj Nothing) =
72 printBinds [] = putStr "done\n\n"
73 printBinds (b:bs) = do
78 printBind (NonRec b expr) = do
82 printBind (Rec binds) = do
84 foldl1 (>>) (map printBind' binds)
86 printBind' (b, expr) = do
87 putStr $ getOccString b
88 putStr $ showSDoc $ ppr expr
91 findBind :: [CoreBind] -> String -> Maybe CoreBind
92 findBind binds lookfor =
93 -- This ignores Recs and compares the name of the bind with lookfor,
94 -- disregarding any namespaces in OccName and extra attributes in Name and
98 NonRec var _ -> lookfor == (occNameString $ nameOccName $ getName var)
102 SignalNameMap -- The port name to bind to
104 -- The signal or port to bind to it
105 -> AST.AssocElem -- The resulting port map entry
107 -- Accepts a port name and an argument to map to it.
108 -- Returns the appropriate line for in the port map
109 getPortMapEntry (Signal portname _) (Signal signame _) =
110 (Just portname) AST.:=>: (AST.ADName (AST.NSimple signame))
113 [SignalNameMap] -- The arguments that need to be applied to the
115 -> SignalNameMap -- The output ports that the expression should generate.
116 -> [(CoreBndr, SignalNameMap)]
117 -- A list of bindings in effect
118 -> CoreSyn.CoreExpr -- The expression to generate an architecture for
119 -> VHDLState ([AST.SigDec], [AST.ConcSm])
120 -- The resulting VHDL code
122 -- A lambda expression binds the first argument (a) to the binder b.
123 getInstantiations (a:as) outs binds (Lam b expr) =
124 getInstantiations as outs ((b, a):binds) expr
126 -- A case expression that checks a single variable and has a single
127 -- alternative, can be used to take tuples apart
128 getInstantiations args outs binds (Case (Var v) b _ [res]) =
129 -- Split out the type of alternative constructor, the variables it binds
130 -- and the expression to evaluate with the variables bound.
131 let (altcon, bind_vars, expr) = res in
134 if (DataCon.isTupleCon datacon) then
136 -- Lookup the scrutinee (which must be a variable bound to a tuple) in
137 -- the existing bindings list and get the portname map for each of
139 Tuple tuple_ports = Maybe.fromMaybe
140 (error $ "Case expression uses unknown scrutinee " ++ getOccString v)
142 -- Merge our existing binds with the new binds.
143 binds' = (zip bind_vars tuple_ports) ++ binds
145 -- Evaluate the expression with the new binds list
146 getInstantiations args outs binds' expr
148 error "Data constructors other than tuples not supported"
150 error "Case binders other than tuples not supported"
152 -- An application is an instantiation of a component
153 getInstantiations args outs binds app@(App expr arg) = do
154 let ((Var f), fargs) = collectArgs app
155 name = getOccString f
156 if isTupleConstructor f
158 -- Get the signals we should bind our results to
159 let Tuple outports = outs
160 -- Split the tuple constructor arguments into types and actual values.
161 let (_, vals) = splitTupleConstructorArgs fargs
162 -- Bind each argument to each output signal
163 res <- sequence $ zipWith
164 (\outs' expr' -> getInstantiations args outs' binds expr')
166 -- res is a list of pairs of lists, so split out the signals and
167 -- components into separate lists of lists
168 let (sigs, comps) = unzip res
169 -- And join all the signals and component instantiations together
170 return $ (concat sigs, concat comps)
172 -- This is an normal function application, which maps to a component
174 -- Lookup the hwfunction to instantiate
175 HWFunction vhdl_id inports outport <- getHWFunc name
176 -- Generate a unique name for the application
177 appname <- uniqueName "app"
178 -- Expand each argument to a signal or port name, possibly generating
179 -- new signals and component instantiations
180 (sigs, comps, args) <- expandArgs binds fargs
181 -- Bind each of the input ports to the expanded signal or port
182 let inmaps = zipWith getPortMapEntry inports args
183 -- Bind each of the output ports to our output signals
184 let outmaps = mapOutputPorts outport outs
185 -- Build and return a component instantiation
186 let comp = AST.CompInsSm
187 (AST.unsafeVHDLBasicId appname)
188 (AST.IUEntity (AST.NSimple vhdl_id))
189 (AST.PMapAspect (inmaps ++ outmaps))
190 return (sigs, (AST.CSISm comp) : comps)
192 getInstantiations args outs binds expr =
193 error $ "Unsupported expression" ++ (showSDoc $ ppr $ expr)
196 [(CoreBndr, SignalNameMap)]
197 -- A list of bindings in effect
198 -> CoreExpr -- The expression to expand
200 [AST.SigDec], -- Needed signal declarations
201 [AST.ConcSm], -- Needed component instantations and
202 -- signal assignments.
203 [SignalNameMap], -- The signal names corresponding to
204 -- the expression's arguments
205 SignalNameMap) -- The signal names corresponding to
206 -- the expression's result.
207 expandExpr binds lam@(Lam b expr) = do
208 -- Generate a new signal to which we will expect this argument to be bound.
209 signal_name <- uniqueName ("arg-" ++ getOccString b)
210 -- Find the type of the binder
211 let (arg_ty, _) = Type.splitFunTy (CoreUtils.exprType lam)
212 -- Create signal names for the binder
213 let arg_signal = getPortNameMapForTy ("xxx") arg_ty
214 -- Create the corresponding signal declarations
215 let signal_decls = mkSignalsFromMap arg_signal
216 -- Add the binder to the list of binds
217 let binds' = (b, arg_signal) : binds
218 -- Expand the rest of the expression
219 (signal_decls', statements', arg_signals', res_signal') <- expandExpr binds' expr
220 -- Properly merge the results
221 return (signal_decls ++ signal_decls',
223 arg_signal : arg_signals',
226 expandExpr binds (Var id) =
227 return ([], [], [], Signal signal_id ty)
229 -- Lookup the id in our binds map
230 Signal signal_id ty = Maybe.fromMaybe
231 (error $ "Argument " ++ getOccString id ++ "is unknown")
234 expandExpr binds l@(Let (NonRec b bexpr) expr) = do
235 (signal_decls, statements, arg_signals, res_signals) <- expandExpr binds bexpr
236 let binds' = (b, res_signals) : binds
237 (signal_decls', statements', arg_signals', res_signals') <- expandExpr binds' expr
239 signal_decls ++ signal_decls',
240 statements ++ statements',
244 expandExpr binds app@(App _ _) = do
245 let ((Var f), args) = collectArgs app
246 if isTupleConstructor f
248 expandBuildTupleExpr binds args
250 expandApplicationExpr binds (CoreUtils.exprType app) f args
252 expandExpr binds expr@(Case (Var v) b _ alts) =
254 [alt] -> expandSingleAltCaseExpr binds v b alt
255 otherwise -> error $ "Multiple alternative case expression not supported: " ++ (showSDoc $ ppr expr)
257 expandExpr binds expr@(Case _ b _ _) =
258 error $ "Case expression with non-variable scrutinee not supported: " ++ (showSDoc $ ppr expr)
260 expandExpr binds expr =
261 error $ "Unsupported expression: " ++ (showSDoc $ ppr $ expr)
263 -- Expands the construction of a tuple into VHDL
264 expandBuildTupleExpr ::
265 [(CoreBndr, SignalNameMap)]
266 -- A list of bindings in effect
267 -> [CoreExpr] -- A list of expressions to put in the tuple
268 -> VHDLState ( [AST.SigDec], [AST.ConcSm], [SignalNameMap], SignalNameMap)
270 expandBuildTupleExpr binds args = do
271 -- Split the tuple constructor arguments into types and actual values.
272 let (_, vals) = splitTupleConstructorArgs args
273 -- Expand each of the values in the tuple
274 (signals_declss, statementss, arg_signalss, res_signals) <-
275 (Monad.liftM List.unzip4) $ mapM (expandExpr binds) vals
276 if any (not . null) arg_signalss
277 then error "Putting high order functions in tuples not supported"
280 concat signals_declss,
285 -- Expands the most simple case expression that scrutinizes a plain variable
286 -- and has a single alternative. This simple form currently allows only for
287 -- unpacking tuple variables.
288 expandSingleAltCaseExpr ::
289 [(CoreBndr, SignalNameMap)]
290 -- A list of bindings in effect
291 -> Var.Var -- The scrutinee
292 -> CoreBndr -- The binder to bind the scrutinee to
293 -> CoreAlt -- The single alternative
294 -> VHDLState ( [AST.SigDec], [AST.ConcSm], [SignalNameMap], SignalNameMap)
297 expandSingleAltCaseExpr binds v b alt@(DataAlt datacon, bind_vars, expr) =
298 if not (DataCon.isTupleCon datacon)
300 error $ "Dataconstructors other than tuple constructors not supported in case pattern of alternative: " ++ (showSDoc $ ppr alt)
303 -- Lookup the scrutinee (which must be a variable bound to a tuple) in
304 -- the existing bindings list and get the portname map for each of
306 Tuple tuple_ports = Maybe.fromMaybe
307 (error $ "Case expression uses unknown scrutinee " ++ getOccString v)
309 -- TODO include b in the binds list
310 -- Merge our existing binds with the new binds.
311 binds' = (zip bind_vars tuple_ports) ++ binds
313 -- Expand the expression with the new binds list
314 expandExpr binds' expr
316 expandSingleAltCaseExpr _ _ _ alt =
317 error $ "Case patterns other than data constructors not supported in case alternative: " ++ (showSDoc $ ppr alt)
320 -- Expands the application of argument to a function into VHDL
321 expandApplicationExpr ::
322 [(CoreBndr, SignalNameMap)]
323 -- A list of bindings in effect
324 -> Type -- The result type of the function call
325 -> Var.Var -- The function to call
326 -> [CoreExpr] -- A list of argumetns to apply to the function
327 -> VHDLState ( [AST.SigDec], [AST.ConcSm], [SignalNameMap], SignalNameMap)
329 expandApplicationExpr binds ty f args = do
330 let name = getOccString f
331 -- Generate a unique name for the application
332 appname <- uniqueName ("app-" ++ name)
333 -- Lookup the hwfunction to instantiate
334 HWFunction vhdl_id inports outport <- getHWFunc name
335 -- Expand each of the args, so each of them is reduced to output signals
336 (arg_signal_decls, arg_statements, arg_res_signals) <- expandArgs binds args
337 -- Bind each of the input ports to the expanded arguments
338 let inmaps = concat $ zipWith createAssocElems inports arg_res_signals
339 -- Create signal names for our result
340 let res_signal = getPortNameMapForTy (appname ++ "-out") ty
341 -- Create the corresponding signal declarations
342 let signal_decls = mkSignalsFromMap res_signal
343 -- Bind each of the output ports to our output signals
344 let outmaps = mapOutputPorts outport res_signal
345 -- Instantiate the component
346 let component = AST.CSISm $ AST.CompInsSm
347 (AST.unsafeVHDLBasicId appname)
348 (AST.IUEntity (AST.NSimple vhdl_id))
349 (AST.PMapAspect (inmaps ++ outmaps))
350 -- Merge the generated declarations
352 signal_decls ++ arg_signal_decls,
353 component : arg_statements,
354 [], -- We don't take any extra arguments; we don't support higher order functions yet
357 -- Creates a list of AssocElems (port map lines) that maps the given signals
358 -- to the given ports.
360 SignalNameMap -- The port names to bind to
361 -> SignalNameMap -- The signals to bind to it
362 -> [AST.AssocElem] -- The resulting port map lines
364 createAssocElems (Signal port_id _) (Signal signal_id _) =
365 [(Just port_id) AST.:=>: (AST.ADName (AST.NSimple signal_id))]
367 createAssocElems (Tuple ports) (Tuple signals) =
368 concat $ zipWith createAssocElems ports signals
370 -- Generate a signal declaration for a signal with the given name and the
371 -- given type and no value. Also returns the id of the signal.
372 mkSignal :: String -> AST.TypeMark -> (AST.VHDLId, AST.SigDec)
374 (id, mkSignalFromId id ty)
376 id = AST.unsafeVHDLBasicId name
378 mkSignalFromId :: AST.VHDLId -> AST.TypeMark -> AST.SigDec
379 mkSignalFromId id ty =
380 AST.SigDec id ty Nothing
382 -- Generates signal declarations for all the signals in the given map
387 mkSignalsFromMap (Signal id ty) =
388 [mkSignalFromId id ty]
390 mkSignalsFromMap (Tuple signals) =
391 concat $ map mkSignalsFromMap signals
394 [(CoreBndr, SignalNameMap)] -- A list of bindings in effect
395 -> [CoreExpr] -- The arguments to expand
396 -> VHDLState ([AST.SigDec], [AST.ConcSm], [SignalNameMap])
397 -- The resulting signal declarations,
398 -- component instantiations and a
399 -- VHDLName for each of the
400 -- expressions passed in.
401 expandArgs binds (e:exprs) = do
402 -- Expand the first expression
403 (signal_decls, statements, arg_signals, res_signal) <- expandExpr binds e
404 if not (null arg_signals)
405 then error $ "Passing functions as arguments not supported: " ++ (showSDoc $ ppr e)
407 (signal_decls', statements', res_signals') <- expandArgs binds exprs
409 signal_decls ++ signal_decls',
410 statements ++ statements',
411 res_signal : res_signals')
413 expandArgs _ [] = return ([], [], [])
415 -- Is the given name a (binary) tuple constructor
416 isTupleConstructor :: Var.Var -> Bool
417 isTupleConstructor var =
418 Name.isWiredInName name
419 && Name.nameModule name == tuple_mod
420 && (Name.occNameString $ Name.nameOccName name) == "(,)"
422 name = Var.varName var
423 mod = nameModule name
424 tuple_mod = Module.mkModule (Module.stringToPackageId "ghc-prim") (Module.mkModuleName "GHC.Tuple")
426 -- Split arguments into type arguments and value arguments This is probably
427 -- not really sufficient (not sure if Types can actually occur as value
429 splitTupleConstructorArgs :: [CoreExpr] -> ([CoreExpr], [CoreExpr])
430 splitTupleConstructorArgs (e:es) =
432 Type t -> (e:tys, vals)
433 otherwise -> (tys, e:vals)
435 (tys, vals) = splitTupleConstructorArgs es
437 splitTupleConstructorArgs [] = ([], [])
440 SignalNameMap -- The output portnames of the component
441 -> SignalNameMap -- The output portnames and/or signals to map these to
442 -> [AST.AssocElem] -- The resulting output ports
444 -- Map the output port of a component to the output port of the containing
446 mapOutputPorts (Signal portname _) (Signal signalname _) =
447 [(Just portname) AST.:=>: (AST.ADName (AST.NSimple signalname))]
449 -- Map matching output ports in the tuple
450 mapOutputPorts (Tuple ports) (Tuple signals) =
451 concat (zipWith mapOutputPorts ports signals)
454 CoreBind -- The binder to expand into an architecture
455 -> VHDLState AST.ArchBody -- The resulting architecture
457 getArchitecture (Rec _) = error "Recursive binders not supported"
459 getArchitecture (NonRec var expr) = do
460 let name = (getOccString var)
461 HWFunction vhdl_id inports outport <- getHWFunc name
463 (signal_decls, statements, arg_signals, res_signal) <- expandExpr [] expr
464 let inport_assigns = concat $ zipWith createSignalAssignments arg_signals inports
465 let outport_assigns = createSignalAssignments outport res_signal
466 return $ AST.ArchBody
467 (AST.unsafeVHDLBasicId "structural")
468 (AST.NSimple vhdl_id)
469 (map AST.BDISD signal_decls)
470 (inport_assigns ++ outport_assigns ++ statements)
472 -- Generate a VHDL entity declaration for the given function
473 getEntity :: HWFunction -> AST.EntityDec
474 getEntity (HWFunction vhdl_id inports outport) =
475 AST.EntityDec vhdl_id ports
478 (concat $ map (mkIfaceSigDecs AST.In) inports)
479 ++ mkIfaceSigDecs AST.Out outport
482 AST.Mode -- The port's mode (In or Out)
483 -> SignalNameMap -- The ports to generate a map for
484 -> [AST.IfaceSigDec] -- The resulting ports
486 mkIfaceSigDecs mode (Signal port_id ty) =
487 [AST.IfaceSigDec port_id mode ty]
489 mkIfaceSigDecs mode (Tuple ports) =
490 concat $ map (mkIfaceSigDecs mode) ports
492 -- Create concurrent assignments of one map of signals to another. The maps
493 -- should have a similar form.
494 createSignalAssignments ::
495 SignalNameMap -- The signals to assign to
496 -> SignalNameMap -- The signals to assign
497 -> [AST.ConcSm] -- The resulting assignments
499 -- A simple assignment of one signal to another (greatly complicated because
500 -- signal assignments can be conditional with multiple conditions in VHDL).
501 createSignalAssignments (Signal dst _) (Signal src _) =
504 src_name = AST.NSimple src
505 src_expr = AST.PrimName src_name
506 src_wform = AST.Wform [AST.WformElem src_expr Nothing]
507 dst_name = (AST.NSimple dst)
508 assign = dst_name AST.:<==: (AST.ConWforms [] src_wform Nothing)
510 createSignalAssignments (Tuple dsts) (Tuple srcs) =
511 concat $ zipWith createSignalAssignments dsts srcs
513 createSignalAssignments dst src =
514 error $ "Non matching source and destination: " ++ show dst ++ "\nand\n" ++ show src
517 Tuple [SignalNameMap]
518 | Signal AST.VHDLId AST.TypeMark -- A signal (or port) of the given (VDHL) type
521 -- Generate a port name map (or multiple for tuple types) in the given direction for
523 getPortNameMapForTys :: String -> Int -> [Type] -> [SignalNameMap]
524 getPortNameMapForTys prefix num [] = []
525 getPortNameMapForTys prefix num (t:ts) =
526 (getPortNameMapForTy (prefix ++ show num) t) : getPortNameMapForTys prefix (num + 1) ts
528 getPortNameMapForTy :: String -> Type -> SignalNameMap
529 getPortNameMapForTy name ty =
530 if (TyCon.isTupleTyCon tycon) then
531 -- Expand tuples we find
532 Tuple (getPortNameMapForTys name 0 args)
533 else -- Assume it's a type constructor application, ie simple data type
534 -- TODO: Don't hardcode the type here
535 Signal (AST.unsafeVHDLBasicId name) vhdl_bit_ty
537 (tycon, args) = Type.splitTyConApp ty
539 data HWFunction = HWFunction { -- A function that is available in hardware
540 vhdlId :: AST.VHDLId,
541 inPorts :: [SignalNameMap],
542 outPort :: SignalNameMap
543 --entity :: AST.EntityDec
546 -- Turns a CoreExpr describing a function into a description of its input and
549 CoreBind -- The core binder to generate the interface for
550 -> VHDLState (String, HWFunction) -- The name of the function and its interface
552 mkHWFunction (NonRec var expr) =
553 return (name, HWFunction (mkVHDLId name) inports outport)
555 name = getOccString var
556 ty = CoreUtils.exprType expr
557 (fargs, res) = Type.splitFunTys ty
558 args = if length fargs == 1 then fargs else (init fargs)
559 --state = if length fargs == 1 then () else (last fargs)
560 inports = case args of
561 -- Handle a single port specially, to prevent an extra 0 in the name
562 [port] -> [getPortNameMapForTy "portin" port]
563 ps -> getPortNameMapForTys "portin" 0 ps
564 outport = getPortNameMapForTy "portout" res
566 mkHWFunction (Rec _) =
567 error "Recursive binders not supported"
569 data VHDLSession = VHDLSession {
570 nameCount :: Int, -- A counter that can be used to generate unique names
571 funcs :: [(String, HWFunction)] -- All functions available, indexed by name
574 type VHDLState = State.State VHDLSession
576 -- Add the function to the session
577 addFunc :: String -> HWFunction -> VHDLState ()
579 fs <- State.gets funcs -- Get the funcs element from the session
580 State.modify (\x -> x {funcs = (name, f) : fs }) -- Prepend name and f
582 -- Lookup the function with the given name in the current session. Errors if
584 getHWFunc :: String -> VHDLState HWFunction
586 fs <- State.gets funcs -- Get the funcs element from the session
587 return $ Maybe.fromMaybe
588 (error $ "Function " ++ name ++ "is unknown? This should not happen!")
591 -- Makes the given name unique by appending a unique number.
592 -- This does not do any checking against existing names, so it only guarantees
593 -- uniqueness with other names generated by uniqueName.
594 uniqueName :: String -> VHDLState String
596 count <- State.gets nameCount -- Get the funcs element from the session
597 State.modify (\s -> s {nameCount = count + 1})
598 return $ name ++ "-" ++ (show count)
601 mkVHDLId :: String -> AST.VHDLId
602 mkVHDLId = AST.unsafeVHDLBasicId
606 ("hwxor", HWFunction (mkVHDLId "hwxor") [Signal (mkVHDLId "a") vhdl_bit_ty, Signal (mkVHDLId "b") vhdl_bit_ty] (Signal (mkVHDLId "o") vhdl_bit_ty)),
607 ("hwand", HWFunction (mkVHDLId "hwand") [Signal (mkVHDLId "a") vhdl_bit_ty, Signal (mkVHDLId "b") vhdl_bit_ty] (Signal (mkVHDLId "o") vhdl_bit_ty)),
608 ("hwor", HWFunction (mkVHDLId "hwor") [Signal (mkVHDLId "a") vhdl_bit_ty, Signal (mkVHDLId "b") vhdl_bit_ty] (Signal (mkVHDLId "o") vhdl_bit_ty)),
609 ("hwnot", HWFunction (mkVHDLId "hwnot") [Signal (mkVHDLId "i") vhdl_bit_ty] (Signal (mkVHDLId "o") vhdl_bit_ty))
612 vhdl_bit_ty :: AST.TypeMark
613 vhdl_bit_ty = AST.unsafeVHDLBasicId "Bit"
615 -- vim: set ts=8 sw=2 sts=2 expandtab: