From: Christiaan Baaij Date: Fri, 19 Jun 2009 11:25:24 +0000 (+0200) Subject: Merge branch 'cλash' of http://git.stderr.nl/matthijs/projects/master-project X-Git-Url: https://git.stderr.nl/gitweb?a=commitdiff_plain;h=8a17f35807fb35ee4d2a4c35c75e1cf99066f94d;hp=ff9a8487475aa90d2f212fd24169503993a4a27d;p=matthijs%2Fmaster-project%2Fc%CE%BBash.git Merge branch 'cλash' of git.stderr.nl/matthijs/projects/master-project * 'cλash' of http://git.stderr.nl/matthijs/projects/master-project: Add another higher order testcase, highordtest2. Support VHDL generation for two-alternative cases. Conflicts: Translator.hs --- diff --git a/Adders.hs b/Adders.hs index ebc1c8c..4b18e39 100644 --- a/Adders.hs +++ b/Adders.hs @@ -174,6 +174,16 @@ instance Inv (BitVec D0) where functiontest :: TFVec D4 Bit -> Bit functiontest = \v -> let r = head v in r +highordtest2 = \a b -> + case a of + High -> \c d -> d + Low -> let + op' :: Bit -> Bit -> Bit + op' = case b of + High -> \c d -> d + Low -> \c d -> c + in + \c d -> op' d c -- Four bit adder, using the continous adder below -- [a] -> [b] -> ([s], cout) --con_adder_4 as bs = diff --git a/Translator.hs b/Translator.hs index 0f60277..1786332 100644 --- a/Translator.hs +++ b/Translator.hs @@ -52,8 +52,8 @@ import FlattenTypes import VHDLTypes import qualified VHDL --- main = do --- makeVHDL "Alu.hs" "exec" True +main = do + makeVHDL "Adders.hs" "highordtest2" True makeVHDL :: String -> String -> Bool -> IO () makeVHDL filename name stateful = do diff --git a/VHDL.hs b/VHDL.hs index 319b5b7..8eb130f 100644 --- a/VHDL.hs +++ b/VHDL.hs @@ -22,12 +22,13 @@ import Debug.Trace import qualified ForSyDe.Backend.VHDL.AST as AST -- GHC API +import CoreSyn import qualified Type import qualified Name import qualified OccName import qualified Var import qualified TyCon -import qualified CoreSyn +import qualified DataCon import Outputable ( showSDoc, ppr ) -- Local imports @@ -278,6 +279,50 @@ mkConcSm (bndr, app@(CoreSyn.App _ _))= do -- least compile for now. mkConcSm (bndr, CoreSyn.Var _) = return $ AST.CSPSm $ AST.ProcSm (mkVHDLBasicId "unused") [] [] +-- A single alt case must be a selector +mkConcSm (bndr, (Case (Var scrut) b ty [alt])) = error "Single case alt not supported yet" + +-- Multiple case alt are be conditional assignments and have only wild +-- binders in the alts and only variables in the case values and a variable +-- for a scrutinee. We check the constructor of the second alt, since the +-- first is the default case, if there is any. +mkConcSm (bndr, (Case (Var scrut) b ty [(_, _, Var false), (con, _, Var true)])) = + let + cond_expr = (varToVHDLExpr scrut) AST.:=: (conToVHDLExpr con) + true_expr = (varToVHDLExpr true) + false_expr = (varToVHDLExpr false) + false_wform = AST.Wform [AST.WformElem false_expr Nothing] + true_wform = AST.Wform [AST.WformElem true_expr Nothing] + whenelse = AST.WhenElse true_wform cond_expr + dst_name = AST.NSimple (bndrToVHDLId bndr) + assign = dst_name AST.:<==: (AST.ConWforms [whenelse] false_wform Nothing) + in + return $ AST.CSSASm assign +mkConcSm (_, (Case (Var _) _ _ alts)) = error "VHDL.mkConcSm Not in normal form: Case statement with more than two alternatives" +mkConcSm (_, Case _ _ _ _) = error "VHDL.mkConcSm Not in normal form: Case statement has does not have a simple variable as scrutinee" + +-- Turn a variable reference into a AST expression +varToVHDLExpr :: Var.Var -> AST.Expr +varToVHDLExpr var = AST.PrimName $ AST.NSimple $ bndrToVHDLId var + +-- Turn a constructor into an AST expression. For dataconstructors, this is +-- only the constructor itself, not any arguments it has. Should not be called +-- with a DEFAULT constructor. +conToVHDLExpr :: CoreSyn.AltCon -> AST.Expr +conToVHDLExpr (DataAlt dc) = AST.PrimLit lit + where + tycon = DataCon.dataConTyCon dc + tyname = TyCon.tyConName tycon + dcname = DataCon.dataConName dc + lit = case Name.getOccString tyname of + -- TODO: Do something more robust than string matching + "Bit" -> case Name.getOccString dcname of "High" -> "'1'"; "Low" -> "'0'" + "Bool" -> case Name.getOccString dcname of "True" -> "true"; "False" -> "false" +conToVHDLExpr (LitAlt _) = error "VHDL.conToVHDLExpr Literals not support in case alternatives yet" +conToVHDLExpr DEFAULT = error "VHDL.conToVHDLExpr DEFAULT alternative should not occur here!" + + + {- mkConcSm sigs (UncondDef src dst) _ = do src_expr <- vhdl_expr src