1 module Translator 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.VHDL.FileIO
30 import qualified ForSyDe.Backend.Ppr
31 -- This is needed for rendering the pretty printed VHDL
32 import Text.PrettyPrint.HughesPJ (render)
34 import TranslatorTypes
41 defaultErrorHandler defaultDynFlags $ do
42 runGhc (Just libdir) $ do
43 dflags <- getSessionDynFlags
44 setSessionDynFlags dflags
45 --target <- guessTarget "adder.hs" Nothing
46 --liftIO (print (showSDoc (ppr (target))))
47 --liftIO $ printTarget target
50 --core <- GHC.compileToCoreSimplified "Adders.hs"
51 core <- GHC.compileToCoreSimplified "Adders.hs"
52 --liftIO $ printBinds (cm_binds core)
53 let binds = Maybe.mapMaybe (findBind (cm_binds core)) ["sfull_adder"]
54 liftIO $ putStr $ prettyShow binds
55 -- Turn bind into VHDL
56 let (vhdl, sess) = State.runState (mkVHDL binds) (VHDLSession core 0 [])
57 liftIO $ putStr $ render $ ForSyDe.Backend.Ppr.ppr vhdl
58 liftIO $ ForSyDe.Backend.VHDL.FileIO.writeDesignFile vhdl "../vhdl/vhdl/output.vhdl"
59 liftIO $ putStr $ "\n\nFinal session:\n" ++ prettyShow sess ++ "\n\n"
62 -- Turns the given bind into VHDL
64 -- Add the builtin functions
65 --mapM (uncurry addFunc) builtin_funcs
66 -- Create entities and architectures for them
67 mapM flattenBind binds
68 return $ AST.DesignFile
72 findBind :: [CoreBind] -> String -> Maybe CoreBind
73 findBind binds lookfor =
74 -- This ignores Recs and compares the name of the bind with lookfor,
75 -- disregarding any namespaces in OccName and extra attributes in Name and
79 NonRec var _ -> lookfor == (occNameString $ nameOccName $ getName var)
82 -- | Flattens the given bind and adds it to the session. Then (recursively)
83 -- finds any functions it uses and does the same with them.
85 CoreBind -- The binder to flatten
88 flattenBind (Rec _) = error "Recursive binders not supported"
90 flattenBind bind@(NonRec var expr) = do
91 -- Create the function signature
92 let ty = CoreUtils.exprType expr
93 let hsfunc = mkHsFunction var ty
94 --hwfunc <- mkHWFunction bind hsfunc
95 -- Add it to the session
96 --addFunc hsfunc hwfunc
97 let flatfunc = flattenFunction hsfunc bind
98 addFunc hsfunc flatfunc
99 let used_hsfuncs = map appFunc (apps flatfunc)
100 State.mapM resolvFunc used_hsfuncs
103 -- | Find the given function, flatten it and add it to the session. Then
104 -- (recursively) do the same for any functions used.
106 HsFunction -- | The function to look for
109 resolvFunc hsfunc = do
110 -- See if the function is already known
111 func <- getFunc hsfunc
113 -- Already known, do nothing
116 -- New function, resolve it
118 -- Get the current module
120 -- Find the named function
121 let bind = findBind (cm_binds core) name
123 Nothing -> error $ "Couldn't find function " ++ name ++ " in current module."
124 Just b -> flattenBind b
126 name = hsFuncName hsfunc
128 -- | Translate a top level function declaration to a HsFunction. i.e., which
129 -- interface will be provided by this function. This function essentially
130 -- defines the "calling convention" for hardware models.
132 Var.Var -- ^ The function defined
133 -> Type -- ^ The function type (including arguments!)
134 -> HsFunction -- ^ The resulting HsFunction
137 HsFunction hsname hsargs hsres
139 hsname = getOccString f
140 (arg_tys, res_ty) = Type.splitFunTys ty
141 -- The last argument must be state
142 state_ty = last arg_tys
143 state = useAsState (mkHsValueMap state_ty)
144 -- All but the last argument are inports
145 inports = map (useAsPort . mkHsValueMap)(init arg_tys)
146 hsargs = inports ++ [state]
147 hsres = case splitTupleType res_ty of
148 -- Result type must be a two tuple (state, ports)
149 Just [outstate_ty, outport_ty] -> if Type.coreEqType state_ty outstate_ty
151 Tuple [state, useAsPort (mkHsValueMap outport_ty)]
153 error $ "Input state type of function " ++ hsname ++ ": " ++ (showSDoc $ ppr state_ty) ++ " does not match output state type: " ++ (showSDoc $ ppr outstate_ty)
154 otherwise -> error $ "Return type of top-level function " ++ hsname ++ " must be a two-tuple containing a state and output ports."
156 -- | Splits a tuple type into a list of element types, or Nothing if the type
157 -- is not a tuple type.
159 Type -- ^ The type to split
160 -> Maybe [Type] -- ^ The tuples element types
163 case Type.splitTyConApp_maybe ty of
164 Just (tycon, args) -> if TyCon.isTupleTyCon tycon
171 -- vim: set ts=8 sw=2 sts=2 expandtab: