module ForSyDe.Deep.Backend.VHDL.Generate where
import ForSyDe.Deep.Backend.VHDL.Constants
import ForSyDe.Deep.Backend.VHDL.AST
import ForSyDe.Deep.Backend.VHDL.Traverse.VHDLM
genOutAssigns :: [VHDLId] -> [VHDLId] -> [ConcSm]
genOutAssigns = zipWith assign
where assign dest orig = CSSASm $ dest `genSignalAssign` orig
genSignalAssign :: VHDLId
-> VHDLId
-> ConSigAssignSm
genSignalAssign dest orig = genExprAssign dest (PrimName $ NSimple orig)
genFCallAssign :: VHDLId
-> VHDLId
-> [VHDLId]
-> [VHDLId]
-> ConSigAssignSm
genFCallAssign dest fName formal actual =
genExprAssign dest (PrimFCall $ genFCall fName formal actual)
genExprAssign :: VHDLId -> Expr -> ConSigAssignSm
genExprAssign dest origExpr =
NSimple dest :<==: (ConWforms [] (Wform [WformElem origExpr Nothing])
Nothing)
genSysDesignFile :: String -> EntityDec -> LocalTravResult -> DesignFile
genSysDesignFile globalSysId ent@(EntityDec id _) (LocalTravResult decs stms) =
DesignFile contextClause [LUEntity ent, LUArch archBody]
where archBody = ArchBody archId (NSimple id) decs stms
archId = unsafeVHDLBasicId "synthesizable"
libName = globalSysId ++ "_lib"
libId = unsafeVHDLBasicId libName
contextClause = commonContextClause ++
[Library libId,
Use $ NSelected (NSimple libId :.: SSimple typesId) :.: All]
genLibDesignFile :: GlobalTravResult -> DesignFile
genLibDesignFile (GlobalTravResult typeDecs subtypeDecs subProgBodies) =
DesignFile commonContextClause [LUPackageDec packageDec,
LUPackageBody packageBody]
where packageDec = PackageDec typesId (packageUnconsTypeDecs ++
packageSubtypeDecs ++
packageTypeDecs ++
subProgSpecs)
packageUnconsTypeDecs = map PDITD $ filter (\a -> isUnconsType a) typeDecs
packageTypeDecs = map PDITD $ filter (\a -> (not.isUnconsType) a) typeDecs
packageSubtypeDecs = map PDISD subtypeDecs
subProgSpecs = map (\(SubProgBody spec _ _) -> PDISS spec) subProgBodies
packageBody = PackageBody typesId subProgBodies
isUnconsType (TypeDec _ (TDA (UnconsArrayDef _ _))) = True
isUnconsType _ = False
genAssocElems :: [VHDLId] -> [VHDLId] -> [AssocElem]
genAssocElems formalNames actualNames = zipWith genAssoc formalNames actualNames
genPMap :: [VHDLId] -> [VHDLId] -> PMapAspect
genPMap formalIds actualIds =
PMapAspect $ genAssocElems formalIds actualIds
genFCall :: VHDLId -> [VHDLId] -> [VHDLId] -> FCall
genFCall fName formalIds actualIds =
FCall (NSimple fName) $ zipWith genAssoc formalIds actualIds
genExprFCall :: VHDLId -> [Expr] -> Expr
genExprFCall fName args =
PrimFCall $ FCall (NSimple fName) $
map (\exp -> Nothing :=>: ADExpr exp) args
genExprFCallN :: VHDLId -> Int -> [Expr] -> Expr
genExprFCallN fName n args = genExprFCall fName (takeExactly n args)
where takeExactly 0 [] = []
takeExactly n (x:xs) | n > 0 = x : takeExactly (n-1) xs
takeExactly _ _ = error "takeExactly: non exact length of input list"
genExprFCall0 :: VHDLId -> Expr
genExprFCall0 fName = genExprFCall fName []
genExprFCall0L :: VHDLId -> [Expr] -> Expr
genExprFCall0L fName [] = genExprFCall fName []
genExprFCall0L _ _ = error "ForSyDe.Backend.VHDL.Generate.genExprFCall0L incorrect length"
genExprFCall1 :: VHDLId -> Expr -> Expr
genExprFCall1 fName arg = genExprFCall fName [arg]
genExprFCall1L :: VHDLId -> [Expr] -> Expr
genExprFCall1L fName [arg] = genExprFCall fName [arg]
genExprFCall1L _ _ = error "ForSyDe.Backend.VHDL.Generate.genExprFCall1L incorrect length"
genExprFCall2 :: VHDLId -> Expr -> Expr -> Expr
genExprFCall2 fName arg1 arg2 = genExprFCall fName [arg1,arg2]
genExprFCall2L :: VHDLId -> [Expr] -> Expr
genExprFCall2L fName [arg1, arg2] = genExprFCall fName [arg1,arg2]
genExprFCall2L _ _ = error "ForSyDe.Backend.VHDL.Generate.genExprFCall2L incorrect length"
genExprFCall4 :: VHDLId -> Expr -> Expr -> Expr -> Expr -> Expr
genExprFCall4 fName arg1 arg2 arg3 arg4 =
genExprFCall fName [arg1,arg2,arg2,arg3,arg4]
genExprFCall4L :: VHDLId -> [Expr] -> Expr
genExprFCall4L fName [arg1, arg2, arg3, arg4] =
genExprFCall fName [arg1,arg2,arg2,arg3,arg4]
genExprFCall4L _ _ = error "ForSyDe.Backend.VHDL.Generate.genExprFCall4L incorrect length"
genExprProcCall :: VHDLId -> [Expr] -> SeqSm
genExprProcCall pName args = ProcCall (NSimple pName) $
map (\exp -> Nothing :=>: ADExpr exp) args
genExprProcCall0 :: VHDLId -> SeqSm
genExprProcCall0 fName = genExprProcCall fName []
genExprProcCall1 :: VHDLId -> Expr -> SeqSm
genExprProcCall1 pName arg = genExprProcCall pName [arg]
genExprProcCall2 :: VHDLId -> Expr -> Expr -> SeqSm
genExprProcCall2 pName arg1 arg2 = genExprProcCall pName [arg1,arg2]
genExprProcCall4 :: VHDLId -> Expr -> Expr -> Expr -> Expr -> SeqSm
genExprProcCall4 pName arg1 arg2 arg3 arg4 =
genExprProcCall pName [arg1,arg2,arg2,arg3,arg4]
genAssoc :: VHDLId -> VHDLId -> AssocElem
genAssoc formal actual = Just formal :=>: ADName (NSimple actual)
genUnconsVectorFuns :: TypeMark
-> TypeMark
-> [SubProgBody]
genUnconsVectorFuns elemTM vectorTM =
[SubProgBody exSpec [] [exExpr] ,
SubProgBody selSpec [SPVD selVar] [selFor, selRet] ,
SubProgBody emptySpec [SPVD emptyVar] [emptyExpr] ,
SubProgBody lengthSpec [] [lengthExpr] ,
SubProgBody isnullSpec [] [isnullExpr] ,
SubProgBody replaceSpec [SPVD replaceVar] [replaceExpr, replaceRet] ,
SubProgBody headSpec [] [headExpr] ,
SubProgBody lastSpec [] [lastExpr] ,
SubProgBody initSpec [SPVD initVar] [initExpr, initRet] ,
SubProgBody tailSpec [SPVD tailVar] [tailExpr, tailRet] ,
SubProgBody takeSpec [SPVD takeVar] [takeExpr, takeRet] ,
SubProgBody dropSpec [SPVD dropVar] [dropExpr, dropRet] ,
SubProgBody shiftlSpec [SPVD shiftlVar] [shiftlExpr, shiftlRet] ,
SubProgBody shiftrSpec [SPVD shiftrVar] [shiftrExpr, shiftrRet] ,
SubProgBody rotlSpec [SPVD rotlVar] [rotlExpr, rotlRet] ,
SubProgBody rotrSpec [SPVD rotrVar] [rotrExpr, rotrRet] ,
SubProgBody reverseSpec [SPVD reverseVar] [reverseFor, reverseRet] ,
SubProgBody copySpec [SPVD copyVar] [copyExpr] ,
SubProgBody plusgtSpec [SPVD plusgtVar] [plusgtExpr, plusgtRet] ,
SubProgBody ltplusSpec [SPVD ltplusVar] [ltplusExpr, ltplusRet] ,
SubProgBody plusplusSpec [SPVD plusplusVar] [plusplusExpr, plusplusRet] ,
SubProgBody singletonSpec [SPVD singletonVar] [singletonRet] ,
SubProgBody showSpec [SPSB doShowDef] [showRet] ,
SubProgBody defaultSpec [] [defaultExpr] ]
where ixPar = unsafeVHDLBasicId "ix"
vecPar = unsafeVHDLBasicId "vec"
vec1Par = unsafeVHDLBasicId "vec1"
vec2Par = unsafeVHDLBasicId "vec2"
fPar = unsafeVHDLBasicId "f"
nPar = unsafeVHDLBasicId "n"
sPar = unsafeVHDLBasicId "s"
iId = unsafeVHDLBasicId "i"
iPar = iId
aPar = unsafeVHDLBasicId "a"
resId = unsafeVHDLBasicId "res"
exSpec = Function exId [IfaceVarDec vecPar vectorTM,
IfaceVarDec ixPar naturalTM] elemTM
exExpr =
ReturnSm (Just $ PrimName $ NIndexed
(IndexedName (NSimple vecPar) [PrimName $ NSimple ixPar]))
selSpec = Function selId [IfaceVarDec fPar naturalTM,
IfaceVarDec nPar naturalTM,
IfaceVarDec sPar naturalTM,
IfaceVarDec vecPar vectorTM ] vectorTM
selVar =
VarDec resId
(SubtypeIn vectorTM
(Just $ IndexConstraint
[ToRange (PrimLit "0")
((PrimName (NSimple nPar)) :-:
(PrimLit "1")) ]))
Nothing
selFor = ForSM iId (AttribRange $ applyRangeAttrib resId) [selAssign]
selAssign = let origExp = PrimName (NSimple fPar) :+:
(PrimName (NSimple iId) :*:
PrimName (NSimple sPar)) in
NIndexed (IndexedName (NSimple resId) [PrimName (NSimple iId)]) :=
(PrimName $ NIndexed (IndexedName (NSimple vecPar) [origExp]))
selRet = ReturnSm (Just $ PrimName (NSimple resId))
emptySpec = Function emptyId [] vectorTM
emptyVar =
VarDec resId
(SubtypeIn vectorTM
(Just $ IndexConstraint
[ToRange (PrimLit "0")
(PrimLit "-1")]))
Nothing
emptyExpr = ReturnSm (Just $ PrimName (NSimple resId))
lengthSpec = Function lengthId [IfaceVarDec vecPar vectorTM] naturalTM
lengthExpr = ReturnSm (Just $ PrimName (NAttribute $
AttribName (NSimple vecPar) lengthId Nothing))
isnullSpec = Function isnullId [IfaceVarDec vecPar vectorTM] booleanTM
isnullExpr = ReturnSm (Just $
PrimName (NAttribute $
AttribName (NSimple vecPar) lengthId Nothing) :=:
PrimLit "0")
replaceSpec = Function replaceId [IfaceVarDec vecPar vectorTM,
IfaceVarDec iPar naturalTM,
IfaceVarDec aPar elemTM ] vectorTM
replaceVar =
VarDec resId
(SubtypeIn vectorTM
(Just $ IndexConstraint
[ToRange (PrimLit "0")
(PrimName (NAttribute $
AttribName (NSimple vecPar) lengthId Nothing) :-:
(PrimLit "1")) ]))
Nothing
replaceExpr = NSimple resId :=
(vecSlice (PrimLit "0") (PrimName (NSimple iPar) :-: PrimLit "1") :&:
PrimName (NSimple aPar) :&:
vecSlice (PrimName (NSimple iPar) :+: PrimLit "1")
((PrimName (NAttribute $
AttribName (NSimple vecPar) lengthId Nothing))
:-: PrimLit "1"))
replaceRet = ReturnSm (Just $ PrimName $ NSimple resId)
vecSlice init last = PrimName (NSlice
(SliceName
(NSimple vecPar)
(ToRange init last)))
headSpec = Function headId [IfaceVarDec vecPar vectorTM] elemTM
headExpr = ReturnSm (Just $ (PrimName $ NIndexed (IndexedName
(NSimple vecPar) [PrimLit "0"])))
lastSpec = Function lastId [IfaceVarDec vecPar vectorTM] elemTM
lastExpr = ReturnSm (Just $ (PrimName $ NIndexed (IndexedName
(NSimple vecPar)
[PrimName (NAttribute $
AttribName (NSimple vecPar) lengthId Nothing)
:-: PrimLit "1"])))
initSpec = Function initId [IfaceVarDec vecPar vectorTM] vectorTM
initVar =
VarDec resId
(SubtypeIn vectorTM
(Just $ IndexConstraint
[ToRange (PrimLit "0")
(PrimName (NAttribute $
AttribName (NSimple vecPar) lengthId Nothing) :-:
(PrimLit "2")) ]))
Nothing
initExpr = NSimple resId := (vecSlice
(PrimLit "0")
(PrimName (NAttribute $
AttribName (NSimple vecPar) lengthId Nothing)
:-: PrimLit "2"))
initRet = ReturnSm (Just $ PrimName $ NSimple resId)
tailSpec = Function tailId [IfaceVarDec vecPar vectorTM] vectorTM
tailVar =
VarDec resId
(SubtypeIn vectorTM
(Just $ IndexConstraint
[ToRange (PrimLit "0")
(PrimName (NAttribute $
AttribName (NSimple vecPar) lengthId Nothing) :-:
(PrimLit "2")) ]))
Nothing
tailExpr = NSimple resId := (vecSlice
(PrimLit "1")
(PrimName (NAttribute $
AttribName (NSimple vecPar) lengthId Nothing)
:-: PrimLit "1"))
tailRet = ReturnSm (Just $ PrimName $ NSimple resId)
takeSpec = Function takeId [IfaceVarDec nPar naturalTM,
IfaceVarDec vecPar vectorTM ] vectorTM
takeVar =
VarDec resId
(SubtypeIn vectorTM
(Just $ IndexConstraint
[ToRange (PrimLit "0")
((PrimName (NSimple nPar)) :-:
(PrimLit "1")) ]))
Nothing
takeExpr = NSimple resId :=
(vecSlice (PrimLit "1")
(PrimName (NSimple $ nPar) :-: PrimLit "1"))
takeRet = ReturnSm (Just $ PrimName $ NSimple resId)
dropSpec = Function dropId [IfaceVarDec nPar naturalTM,
IfaceVarDec vecPar vectorTM ] vectorTM
dropVar =
VarDec resId
(SubtypeIn vectorTM
(Just $ IndexConstraint
[ToRange (PrimLit "0")
(PrimName (NAttribute $
AttribName (NSimple vecPar) lengthId Nothing) :-:
(PrimName $ NSimple nPar):-: (PrimLit "1")) ]))
Nothing
dropExpr = NSimple resId := (vecSlice
(PrimName $ NSimple nPar)
(PrimName (NAttribute $
AttribName (NSimple vecPar) lengthId Nothing)
:-: PrimLit "1"))
dropRet = ReturnSm (Just $ PrimName $ NSimple resId)
shiftlSpec = Function shiftlId [IfaceVarDec vecPar vectorTM,
IfaceVarDec aPar elemTM ] vectorTM
shiftlVar =
VarDec resId
(SubtypeIn vectorTM
(Just $ IndexConstraint
[ToRange (PrimLit "0")
(PrimName (NAttribute $
AttribName (NSimple vecPar) lengthId Nothing) :-:
(PrimLit "1")) ]))
Nothing
shiftlExpr = NSimple resId :=
(PrimName (NSimple aPar) :&:
genExprFCall1 initId (PrimName $ NSimple vecPar))
shiftlRet = ReturnSm (Just $ PrimName $ NSimple resId)
shiftrSpec = Function shiftrId [IfaceVarDec vecPar vectorTM,
IfaceVarDec aPar elemTM ] vectorTM
shiftrVar =
VarDec resId
(SubtypeIn vectorTM
(Just $ IndexConstraint
[ToRange (PrimLit "0")
(PrimName (NAttribute $
AttribName (NSimple vecPar) lengthId Nothing) :-:
(PrimLit "1")) ]))
Nothing
shiftrExpr = NSimple resId :=
(genExprFCall1 tailId (PrimName $ NSimple vecPar) :&:
PrimName (NSimple aPar))
shiftrRet = ReturnSm (Just $ PrimName $ NSimple resId)
rotlSpec = Function rotlId [IfaceVarDec vecPar vectorTM] vectorTM
rotlVar =
VarDec resId
(SubtypeIn vectorTM
(Just $ IndexConstraint
[ToRange (PrimLit "0")
(PrimName (NAttribute $
AttribName (NSimple vecPar) lengthId Nothing) :-:
(PrimLit "1")) ]))
Nothing
rotlExpr = IfSm (genExprFCall1 isnullId (PrimName $ NSimple vecPar))
[NSimple resId := (PrimName $ NSimple vecPar)]
[]
(Just $ Else [rotlExprRet])
where rotlExprRet =
NSimple resId :=
(genExprFCall1 lastId (PrimName $ NSimple vecPar) :&:
genExprFCall1 initId (PrimName $ NSimple vecPar))
rotlRet = ReturnSm (Just $ PrimName $ NSimple resId)
rotrSpec = Function rotrId [IfaceVarDec vecPar vectorTM] vectorTM
rotrVar =
VarDec resId
(SubtypeIn vectorTM
(Just $ IndexConstraint
[ToRange (PrimLit "0")
(PrimName (NAttribute $
AttribName (NSimple vecPar) lengthId Nothing) :-:
(PrimLit "1")) ]))
Nothing
rotrExpr = IfSm (genExprFCall1 isnullId (PrimName $ NSimple vecPar))
[NSimple resId := (PrimName $ NSimple vecPar)]
[]
(Just $ Else [rotrExprRet])
where rotrExprRet =
NSimple resId :=
(genExprFCall1 lastId (PrimName $ NSimple vecPar) :&:
genExprFCall1 initId (PrimName $ NSimple vecPar))
rotrRet = ReturnSm (Just $ PrimName $ NSimple resId)
reverseSpec = Function reverseId [IfaceVarDec vecPar vectorTM] vectorTM
reverseVar =
VarDec resId
(SubtypeIn vectorTM
(Just $ IndexConstraint
[ToRange (PrimLit "0")
(PrimName (NAttribute $
AttribName (NSimple vecPar) lengthId Nothing) :-:
(PrimLit "1")) ]))
Nothing
reverseFor =
ForSM iId (AttribRange $ applyRangeAttrib resId) [reverseAssign]
reverseAssign = NIndexed (IndexedName (NSimple resId) [destExp]) :=
(PrimName $ NIndexed (IndexedName (NSimple vecPar)
[PrimName $ NSimple iId]))
where destExp = PrimName (NAttribute $ AttribName (NSimple vecPar)
lengthId Nothing) :-:
PrimName (NSimple iId) :-:
(PrimLit "1")
reverseRet = ReturnSm (Just $ PrimName (NSimple resId))
copySpec = Function copyId [IfaceVarDec nPar naturalTM,
IfaceVarDec aPar elemTM ] vectorTM
copyVar =
VarDec resId
(SubtypeIn vectorTM
(Just $ IndexConstraint
[ToRange (PrimLit "0")
((PrimName (NSimple nPar)) :-:
(PrimLit "1")) ]))
(Just $ Aggregate [ElemAssoc (Just Others)
(PrimName $ NSimple aPar)])
copyExpr = ReturnSm (Just $ PrimName $ NSimple resId)
plusgtSpec = Function plusgtId [IfaceVarDec aPar elemTM,
IfaceVarDec vecPar vectorTM] vectorTM
plusgtVar =
VarDec resId
(SubtypeIn vectorTM
(Just $ IndexConstraint
[ToRange (PrimLit "0")
(PrimName (NAttribute $
AttribName (NSimple vecPar) lengthId Nothing))]))
Nothing
plusgtExpr = NSimple resId :=
((PrimName $ NSimple aPar) :&:
(PrimName $ NSimple vecPar))
plusgtRet = ReturnSm (Just $ PrimName $ NSimple resId)
ltplusSpec = Function ltplusId [IfaceVarDec vecPar vectorTM,
IfaceVarDec aPar elemTM] vectorTM
ltplusVar =
VarDec resId
(SubtypeIn vectorTM
(Just $ IndexConstraint
[ToRange (PrimLit "0")
(PrimName (NAttribute $
AttribName (NSimple vecPar) lengthId Nothing))]))
Nothing
ltplusExpr = NSimple resId :=
((PrimName $ NSimple vecPar) :&:
(PrimName $ NSimple aPar))
ltplusRet = ReturnSm (Just $ PrimName $ NSimple resId)
plusplusSpec = Function plusplusId [IfaceVarDec vec1Par vectorTM,
IfaceVarDec vec2Par vectorTM ]
vectorTM
plusplusVar =
VarDec resId
(SubtypeIn vectorTM
(Just $ IndexConstraint
[ToRange (PrimLit "0")
(PrimName (NAttribute $
AttribName (NSimple vec1Par) lengthId Nothing) :+:
PrimName (NAttribute $
AttribName (NSimple vec2Par) lengthId Nothing) :-:
PrimLit "1")]))
Nothing
plusplusExpr = NSimple resId :=
((PrimName $ NSimple vec1Par) :&:
(PrimName $ NSimple vec2Par))
plusplusRet = ReturnSm (Just $ PrimName $ NSimple resId)
singletonSpec = Function singletonId [IfaceVarDec aPar elemTM ]
vectorTM
singletonVar =
VarDec resId
(SubtypeIn vectorTM
(Just $ IndexConstraint
[ToRange (PrimLit "0") (PrimLit "0")]))
(Just $ Aggregate [ElemAssoc (Just Others)
(PrimName $ NSimple aPar)])
singletonRet = ReturnSm (Just $ PrimName $ NSimple resId)
showSpec = Function showId [IfaceVarDec vecPar vectorTM] stringTM
doShowId = unsafeVHDLBasicId "doshow"
doShowDef = SubProgBody doShowSpec [] [doShowRet]
where doShowSpec = Function doShowId [IfaceVarDec vecPar vectorTM]
stringTM
doShowRet =
CaseSm (PrimName (NAttribute $
AttribName (NSimple vecPar) lengthId Nothing))
[CaseSmAlt [ChoiceE $ PrimLit "0"]
[ReturnSm (Just $ PrimLit "\"\"")],
CaseSmAlt [ChoiceE $ PrimLit "1"]
[ReturnSm (Just $
genExprFCall1 showId
(genExprFCall1 headId (PrimName $ NSimple vecPar)) )],
CaseSmAlt [Others]
[ReturnSm (Just $
genExprFCall1 showId
(genExprFCall1 headId (PrimName $ NSimple vecPar)) :&:
PrimLit "','" :&:
genExprFCall1 doShowId
(genExprFCall1 tailId (PrimName $ NSimple vecPar)) ) ]]
showRet = ReturnSm (Just $ PrimLit "'<'" :&:
genExprFCall1 doShowId (PrimName $ NSimple vecPar) :&:
PrimLit "'>'" )
defaultSpec = Function defaultId [] vectorTM
defaultExpr =
ReturnSm (Just $ genExprFCall0 emptyId)
genTupleFuns :: [TypeMark]
-> TypeMark
-> [SubProgBody]
genTupleFuns elemTMs tupleTM =
[SubProgBody defaultSpec [] [defaultExpr],
SubProgBody showSpec [] [showExpr]]
where tupPar = unsafeVHDLBasicId "tup"
defaultSpec = Function defaultId [] tupleTM
defaultExpr =
ReturnSm (Just $ Aggregate (replicate tupSize
(ElemAssoc Nothing $ PrimName defaultSN)))
showSpec = Function showId [IfaceVarDec tupPar tupleTM ] stringTM
showExpr = ReturnSm (Just $
PrimLit "'('" :&: showMiddle :&: PrimLit "')'")
where showMiddle = foldr1 (\e1 e2 -> e1 :&: PrimLit "','" :&: e2) $
map ((genExprFCall1 showId).
PrimName .
NSelected.
(NSimple tupPar:.:).
tupVHDLSuffix)
[1..tupSize]
tupSize = length elemTMs
genAbstExtFuns :: TypeMark
-> TypeMark
-> [SubProgBody]
genAbstExtFuns elemTM absExtTM =
[SubProgBody defaultSpec [] [defaultExpr],
SubProgBody absentSpec [] [absentExpr] ,
SubProgBody presentSpec [] [presentExpr],
SubProgBody fromAbstExtSpec [] [fromAbstExtExpr],
SubProgBody unsafeFromAbstExtSpec [] [unsafeFromAbstExtExpr],
SubProgBody isPresentSpec [] [isPresentExpr],
SubProgBody isAbsentSpec [] [isAbsentExpr],
SubProgBody showSpec [] [showExpr] ]
where defaultPar = unsafeVHDLBasicId "default"
extPar = unsafeVHDLBasicId "extabst"
defaultSpec = Function defaultId [] absExtTM
defaultExpr =
ReturnSm (Just $ PrimName $ NSimple absentId)
absentSpec = Function absentId [] absExtTM
absentExpr =
ReturnSm (Just $ Aggregate
[ElemAssoc Nothing falseExpr,
ElemAssoc Nothing $ PrimName $ defaultSN ])
presentSpec =
Function presentId [IfaceVarDec extPar elemTM] absExtTM
presentExpr =
ReturnSm (Just $ Aggregate [ElemAssoc Nothing trueExpr,
ElemAssoc Nothing $ PrimName $ NSimple extPar ])
fromAbstExtSpec = Function fromAbstExtId [IfaceVarDec defaultPar elemTM,
IfaceVarDec extPar absExtTM]
elemTM
fromAbstExtExpr =
IfSm (PrimName $ NSelected (NSimple extPar :.: SSimple isPresentId))
[ReturnSm (Just $ PrimName $
(NSelected (NSimple extPar :.: SSimple valueId)))]
[]
(Just $ Else
[ReturnSm (Just $ PrimName $ NSimple defaultPar)])
unsafeFromAbstExtSpec =
Function unsafeFromAbstExtId [IfaceVarDec extPar absExtTM] elemTM
unsafeFromAbstExtExpr =
ReturnSm (Just $
PrimName (NSelected (NSimple extPar :.: SSimple valueId)))
isPresentSpec =
Function isPresentId [IfaceVarDec extPar absExtTM] booleanTM
isPresentExpr =
ReturnSm (Just $
PrimName (NSelected (NSimple extPar :.: SSimple isPresentId)))
isAbsentSpec =
Function isAbsentId [IfaceVarDec extPar absExtTM] booleanTM
isAbsentExpr =
ReturnSm (Just $
Not $ PrimName (NSelected (NSimple extPar :.: SSimple isPresentId)))
showSpec = Function showId [IfaceVarDec extPar absExtTM ] stringTM
showExpr =
IfSm (PrimName $ NSelected (NSimple extPar :.: SSimple isPresentId))
[ReturnSm (Just $ PrimLit "\"Prst \"" :&:
genExprFCall1 showId (PrimName $
(NSelected (NSimple extPar :.: SSimple valueId))))]
[]
(Just $ Else
[ReturnSm (Just $ PrimLit "\"Abst\"")])
genEnumAlgFuns :: TypeMark
-> VHDLId
-> [SubProgBody]
genEnumAlgFuns enumTM firstLit =
[SubProgBody defaultSpec [] [defaultExpr],
SubProgBody showSpec [] [showExpr]]
where enumPar = unsafeVHDLBasicId "enum"
defaultSpec = Function defaultId [] enumTM
defaultExpr = ReturnSm (Just $ PrimName (NSimple firstLit))
showSpec = Function showId [IfaceVarDec enumPar enumTM] stringTM
showExpr = ReturnSm (Just $ PrimName $ NSlice $ SliceName image
(ToRange (PrimLit "2")
((PrimName $ NAttribute $
AttribName image lengthId Nothing) :-:
PrimLit "1")))
where image = NAttribute$ AttribName (NSimple enumTM) imageId
(Just $ PrimName $ NSimple enumPar)
applyRangeAttrib :: SimpleName -> AttribName
applyRangeAttrib sName = AttribName (NSimple sName) rangeId Nothing