4 import qualified Control.Monad as Monad
5 import qualified Data.Map as Map
7 import qualified Data.Either as Either
12 import qualified ForSyDe.Backend.VHDL.AST as AST
18 import qualified IdInfo
27 -----------------------------------------------------------------------------
28 -- Functions to generate VHDL for builtin functions
29 -----------------------------------------------------------------------------
31 -- | A function to wrap a builder-like function that expects its arguments to
34 (dst -> func -> [AST.Expr] -> res)
35 -> (dst -> func -> [Either CoreSyn.CoreExpr AST.Expr] -> res)
36 genExprArgs wrap dst func args = wrap dst func args'
37 where args' = map (either (varToVHDLExpr.exprToVar) id) args
39 -- | A function to wrap a builder-like function that expects its arguments to
42 (dst -> func -> [Var.Var] -> res)
43 -> (dst -> func -> [Either CoreSyn.CoreExpr AST.Expr] -> res)
44 genVarArgs wrap dst func args = wrap dst func args'
46 args' = map exprToVar exprargs
47 -- Check (rather crudely) that all arguments are CoreExprs
48 (exprargs, []) = Either.partitionEithers args
50 -- | A function to wrap a builder-like function that produces an expression
51 -- and expects it to be assigned to the destination.
53 ((Either CoreSyn.CoreBndr AST.VHDLName) -> func -> [arg] -> VHDLSession AST.Expr)
54 -> ((Either CoreSyn.CoreBndr AST.VHDLName) -> func -> [arg] -> VHDLSession [AST.ConcSm])
55 genExprRes wrap dst func args = do
56 expr <- wrap dst func args
57 return $ [mkUncondAssign dst expr]
59 -- | Generate a binary operator application. The first argument should be a
60 -- constructor from the AST.Expr type, e.g. AST.And.
61 genOperator2 :: (AST.Expr -> AST.Expr -> AST.Expr) -> BuiltinBuilder
62 genOperator2 op = genExprArgs $ genExprRes (genOperator2' op)
63 genOperator2' :: (AST.Expr -> AST.Expr -> AST.Expr) -> dst -> CoreSyn.CoreBndr -> [AST.Expr] -> VHDLSession AST.Expr
64 genOperator2' op _ f [arg1, arg2] = return $ op arg1 arg2
66 -- | Generate a unary operator application
67 genOperator1 :: (AST.Expr -> AST.Expr) -> BuiltinBuilder
68 genOperator1 op = genExprArgs $ genExprRes (genOperator1' op)
69 genOperator1' :: (AST.Expr -> AST.Expr) -> dst -> CoreSyn.CoreBndr -> [AST.Expr] -> VHDLSession AST.Expr
70 genOperator1' op _ f [arg] = return $ op arg
72 -- | Generate a function call from the destination binder, function name and a
73 -- list of expressions (its arguments)
74 genFCall :: BuiltinBuilder
75 genFCall = genExprArgs $ genExprRes genFCall'
76 genFCall' :: Either CoreSyn.CoreBndr AST.VHDLName -> CoreSyn.CoreBndr -> [AST.Expr] -> VHDLSession AST.Expr
77 genFCall' (Left res) f args = do
78 let fname = varToString f
79 let el_ty = (tfvec_elem . Var.varType) res
80 id <- vectorFunId el_ty fname
81 return $ AST.PrimFCall $ AST.FCall (AST.NSimple id) $
82 map (\exp -> Nothing AST.:=>: AST.ADExpr exp) args
83 genFCall' (Right name) _ _ = error $ "Cannot generate builtin function call assigned to a VHDLName: " ++ show name
85 -- | Generate a generate statement for the builtin function "map"
86 genMap :: BuiltinBuilder
87 genMap = genVarArgs genMap'
88 genMap' :: (Either CoreSyn.CoreBndr AST.VHDLName) -> CoreSyn.CoreBndr -> [Var.Var] -> VHDLSession [AST.ConcSm]
89 genMap' (Left res) f [mapped_f, arg] =
91 -- Setup the generate scheme
92 len = (tfvec_len . Var.varType) res
93 -- TODO: Use something better than varToString
94 label = mkVHDLExtId ("mapVector" ++ (varToString res))
95 n_id = mkVHDLBasicId "n"
96 n_expr = idToVHDLExpr n_id
97 range = AST.ToRange (AST.PrimLit "0") (AST.PrimLit $ show (len-1))
98 genScheme = AST.ForGn n_id range
100 -- Create the content of the generate statement: Applying the mapped_f to
101 -- each of the elements in arg, storing to each element in res
102 resname = mkIndexedName (varToVHDLName res) n_expr
103 argexpr = vhdlNameToVHDLExpr $ mkIndexedName (varToVHDLName arg) n_expr
105 app_concsms <- genApplication (Right resname) mapped_f [Right argexpr]
106 -- Return the generate statement
107 return [AST.CSGSm $ AST.GenerateSm label genScheme [] app_concsms]
109 genMap' (Right name) _ _ = error $ "Cannot generate map function call assigned to a VHDLName: " ++ show name
111 genZipWith :: BuiltinBuilder
112 genZipWith = genVarArgs genZipWith'
113 genZipWith' :: (Either CoreSyn.CoreBndr AST.VHDLName) -> CoreSyn.CoreBndr -> [Var.Var] -> VHDLSession [AST.ConcSm]
114 genZipWith' (Left res) f args@[zipped_f, arg1, arg2] =
116 -- Setup the generate scheme
117 len = (tfvec_len . Var.varType) res
118 -- TODO: Use something better than varToString
119 label = mkVHDLExtId ("zipWithVector" ++ (varToString res))
120 n_id = mkVHDLBasicId "n"
121 n_expr = idToVHDLExpr n_id
122 range = AST.ToRange (AST.PrimLit "0") (AST.PrimLit $ show (len-1))
123 genScheme = AST.ForGn n_id range
125 -- Create the content of the generate statement: Applying the zipped_f to
126 -- each of the elements in arg1 and arg2, storing to each element in res
127 resname = mkIndexedName (varToVHDLName res) n_expr
128 argexpr1 = vhdlNameToVHDLExpr $ mkIndexedName (varToVHDLName arg1) n_expr
129 argexpr2 = vhdlNameToVHDLExpr $ mkIndexedName (varToVHDLName arg2) n_expr
131 app_concsms <- genApplication (Right resname) zipped_f [Right argexpr1, Right argexpr2]
132 -- Return the generate functions
133 return [AST.CSGSm $ AST.GenerateSm label genScheme [] app_concsms]
135 genFoldl :: BuiltinBuilder
136 genFoldl = genFold True
138 genFoldr :: BuiltinBuilder
139 genFoldr = genFold False
141 genFold :: Bool -> BuiltinBuilder
142 genFold left = genVarArgs (genFold' left)
143 genFold' :: Bool -> (Either CoreSyn.CoreBndr AST.VHDLName) -> CoreSyn.CoreBndr -> [Var.Var] -> VHDLSession [AST.ConcSm]
144 -- Special case for an empty input vector, just assign start to res
145 genFold' left (Left res) _ [_, start, vec] | len == 0 = return [mkUncondAssign (Left res) (varToVHDLExpr start)]
146 where len = (tfvec_len . Var.varType) vec
147 genFold' left (Left res) f [folded_f, start, vec] = do
148 -- evec is (TFVec n), so it still needs an element type
149 let (nvec, _) = splitAppTy (Var.varType vec)
150 -- Put the type of the start value in nvec, this will be the type of our
152 let tmp_ty = Type.mkAppTy nvec (Var.varType start)
153 tmp_vhdl_ty <- vhdl_ty tmp_ty
154 -- Setup the generate scheme
155 let gen_label = mkVHDLExtId ("foldlVector" ++ (varToString vec))
156 let block_label = mkVHDLExtId ("foldlVector" ++ (varToString start))
157 let gen_range = if left then AST.ToRange (AST.PrimLit "0") len_min_expr
158 else AST.DownRange len_min_expr (AST.PrimLit "0")
159 let gen_scheme = AST.ForGn n_id gen_range
160 -- Make the intermediate vector
161 let tmp_dec = AST.BDISD $ AST.SigDec tmp_id tmp_vhdl_ty Nothing
162 -- Create the generate statement
163 cells <- sequence [genFirstCell, genOtherCell]
164 let gen_sm = AST.GenerateSm gen_label gen_scheme [] (map AST.CSGSm cells)
165 -- Assign tmp[len-1] or tmp[0] to res
166 let out_assign = mkUncondAssign (Left res) $ vhdlNameToVHDLExpr (if left then
167 (mkIndexedName tmp_name (AST.PrimLit $ show (len-1))) else
168 (mkIndexedName tmp_name (AST.PrimLit "0")))
169 let block = AST.BlockSm block_label [] (AST.PMapAspect []) [tmp_dec] [AST.CSGSm gen_sm, out_assign]
170 return [AST.CSBSm block]
173 len = (tfvec_len . Var.varType) vec
174 -- An id for the counter
175 n_id = mkVHDLBasicId "n"
176 n_cur = idToVHDLExpr n_id
177 -- An expression for previous n
178 n_prev = if left then (n_cur AST.:-: (AST.PrimLit "1"))
179 else (n_cur AST.:+: (AST.PrimLit "1"))
180 -- An expression for len-1
181 len_min_expr = (AST.PrimLit $ show (len-1))
182 -- An id for the tmp result vector
183 tmp_id = mkVHDLBasicId "tmp"
184 tmp_name = AST.NSimple tmp_id
185 -- Generate parts of the fold
186 genFirstCell, genOtherCell :: VHDLSession AST.GenerateSm
188 let cond_label = mkVHDLExtId "firstcell"
189 -- if n == 0 or n == len-1
190 let cond_scheme = AST.IfGn $ n_cur AST.:=: (if left then (AST.PrimLit "0")
191 else (AST.PrimLit $ show (len-1)))
192 -- Output to tmp[current n]
193 let resname = mkIndexedName tmp_name n_cur
195 let argexpr1 = varToVHDLExpr start
196 -- Input from vec[current n]
197 let argexpr2 = vhdlNameToVHDLExpr $ mkIndexedName (varToVHDLName vec) n_cur
198 app_concsms <- genApplication (Right resname) folded_f ( if left then
199 [Right argexpr1, Right argexpr2]
201 [Right argexpr2, Right argexpr1]
203 -- Return the conditional generate part
204 return $ AST.GenerateSm cond_label cond_scheme [] app_concsms
207 let cond_label = mkVHDLExtId "othercell"
208 -- if n > 0 or n < len-1
209 let cond_scheme = AST.IfGn $ n_cur AST.:/=: (if left then (AST.PrimLit "0")
210 else (AST.PrimLit $ show (len-1)))
211 -- Output to tmp[current n]
212 let resname = mkIndexedName tmp_name n_cur
213 -- Input from tmp[previous n]
214 let argexpr1 = vhdlNameToVHDLExpr $ mkIndexedName tmp_name n_prev
215 -- Input from vec[current n]
216 let argexpr2 = vhdlNameToVHDLExpr $ mkIndexedName (varToVHDLName vec) n_cur
217 app_concsms <- genApplication (Right resname) folded_f ( if left then
218 [Right argexpr1, Right argexpr2]
220 [Right argexpr2, Right argexpr1]
222 -- Return the conditional generate part
223 return $ AST.GenerateSm cond_label cond_scheme [] app_concsms
225 -----------------------------------------------------------------------------
226 -- Function to generate VHDL for applications
227 -----------------------------------------------------------------------------
229 (Either CoreSyn.CoreBndr AST.VHDLName) -- ^ Where to store the result?
230 -> CoreSyn.CoreBndr -- ^ The function to apply
231 -> [Either CoreSyn.CoreExpr AST.Expr] -- ^ The arguments to apply
232 -> VHDLSession [AST.ConcSm] -- ^ The resulting concurrent statements
233 genApplication dst f args =
234 case Var.globalIdVarDetails f of
235 IdInfo.DataConWorkId dc -> case dst of
236 -- It's a datacon. Create a record from its arguments.
238 -- We have the bndr, so we can get at the type
239 labels <- getFieldLabels (Var.varType bndr)
240 return $ zipWith mkassign labels $ map (either exprToVHDLExpr id) args
242 mkassign :: AST.VHDLId -> AST.Expr -> AST.ConcSm
244 let sel_name = mkSelectedName ((either varToVHDLName id) dst) label in
245 mkUncondAssign (Right sel_name) arg
246 Right _ -> error $ "Generate.genApplication Can't generate dataconstructor application without an original binder"
247 IdInfo.VanillaGlobal -> do
248 -- It's a global value imported from elsewhere. These can be builtin
249 -- functions. Look up the function name in the name table and execute
250 -- the associated builder if there is any and the argument count matches
251 -- (this should always be the case if it typechecks, but just to be
253 case (Map.lookup (varToString f) globalNameTable) of
254 Just (arg_count, builder) ->
255 if length args == arg_count then
258 error $ "Generate.genApplication Incorrect number of arguments to builtin function: " ++ pprString f ++ " Args: " ++ show args
259 Nothing -> error $ "Using function from another module that is not a known builtin: " ++ pprString f
260 IdInfo.NotGlobalId -> do
261 signatures <- getA vsSignatures
262 -- This is a local id, so it should be a function whose definition we
263 -- have and which can be turned into a component instantiation.
265 signature = Maybe.fromMaybe
266 (error $ "Using function '" ++ (varToString f) ++ "' without signature? This should not happen!")
267 (Map.lookup f signatures)
268 entity_id = ent_id signature
269 -- TODO: Using show here isn't really pretty, but we'll need some
270 -- unique-ish value...
271 label = "comp_ins_" ++ (either show prettyShow) dst
272 portmaps = mkAssocElems (map (either exprToVHDLExpr id) args) ((either varToVHDLName id) dst) signature
274 return [mkComponentInst label entity_id portmaps]
275 details -> error $ "Calling unsupported function " ++ pprString f ++ " with GlobalIdDetails " ++ pprString details
277 -----------------------------------------------------------------------------
278 -- Functions to generate functions dealing with vectors.
279 -----------------------------------------------------------------------------
281 -- Returns the VHDLId of the vector function with the given name for the given
282 -- element type. Generates -- this function if needed.
283 vectorFunId :: Type.Type -> String -> VHDLSession AST.VHDLId
284 vectorFunId el_ty fname = do
285 elemTM <- vhdl_ty el_ty
286 -- TODO: This should not be duplicated from mk_vector_ty. Probably but it in
287 -- the VHDLState or something.
288 let vectorTM = mkVHDLExtId $ "vector_" ++ (AST.fromVHDLId elemTM)
289 typefuns <- getA vsTypeFuns
290 case Map.lookup (OrdType el_ty, fname) typefuns of
291 -- Function already generated, just return it
292 Just (id, _) -> return id
293 -- Function not generated yet, generate it
295 let functions = genUnconsVectorFuns elemTM vectorTM
296 case lookup fname functions of
298 modA vsTypeFuns $ Map.insert (OrdType el_ty, fname) (function_id, body)
300 Nothing -> error $ "I don't know how to generate vector function " ++ fname
302 function_id = mkVHDLExtId fname
304 genUnconsVectorFuns :: AST.TypeMark -- ^ type of the vector elements
305 -> AST.TypeMark -- ^ type of the vector
306 -> [(String, AST.SubProgBody)]
307 genUnconsVectorFuns elemTM vectorTM =
308 [ (exId, AST.SubProgBody exSpec [] [exExpr])
309 , (replaceId, AST.SubProgBody replaceSpec [AST.SPVD replaceVar] [replaceExpr,replaceRet])
310 , (headId, AST.SubProgBody headSpec [] [headExpr])
311 , (lastId, AST.SubProgBody lastSpec [] [lastExpr])
312 , (initId, AST.SubProgBody initSpec [AST.SPVD initVar] [initExpr, initRet])
313 , (tailId, AST.SubProgBody tailSpec [AST.SPVD tailVar] [tailExpr, tailRet])
314 , (takeId, AST.SubProgBody takeSpec [AST.SPVD takeVar] [takeExpr, takeRet])
315 , (dropId, AST.SubProgBody dropSpec [AST.SPVD dropVar] [dropExpr, dropRet])
316 , (plusgtId, AST.SubProgBody plusgtSpec [AST.SPVD plusgtVar] [plusgtExpr, plusgtRet])
317 , (emptyId, AST.SubProgBody emptySpec [AST.SPCD emptyVar] [emptyExpr])
318 , (singletonId, AST.SubProgBody singletonSpec [AST.SPVD singletonVar] [singletonRet])
319 , (copyId, AST.SubProgBody copySpec [AST.SPVD copyVar] [copyExpr])
320 , (selId, AST.SubProgBody selSpec [AST.SPVD selVar] [selFor, selRet])
321 , (ltplusId, AST.SubProgBody ltplusSpec [AST.SPVD ltplusVar] [ltplusExpr, ltplusRet] )
322 , (plusplusId, AST.SubProgBody plusplusSpec [AST.SPVD plusplusVar] [plusplusExpr, plusplusRet])
325 ixPar = AST.unsafeVHDLBasicId "ix"
326 vecPar = AST.unsafeVHDLBasicId "vec"
327 vec1Par = AST.unsafeVHDLBasicId "vec1"
328 vec2Par = AST.unsafeVHDLBasicId "vec2"
329 nPar = AST.unsafeVHDLBasicId "n"
330 iId = AST.unsafeVHDLBasicId "i"
332 aPar = AST.unsafeVHDLBasicId "a"
333 fPar = AST.unsafeVHDLBasicId "f"
334 sPar = AST.unsafeVHDLBasicId "s"
335 resId = AST.unsafeVHDLBasicId "res"
336 exSpec = AST.Function (mkVHDLExtId exId) [AST.IfaceVarDec vecPar vectorTM,
337 AST.IfaceVarDec ixPar naturalTM] elemTM
338 exExpr = AST.ReturnSm (Just $ AST.PrimName $ AST.NIndexed
339 (AST.IndexedName (AST.NSimple vecPar) [AST.PrimName $
341 replaceSpec = AST.Function (mkVHDLExtId replaceId) [ AST.IfaceVarDec vecPar vectorTM
342 , AST.IfaceVarDec iPar naturalTM
343 , AST.IfaceVarDec aPar elemTM
345 -- variable res : fsvec_x (0 to vec'length-1);
348 (AST.SubtypeIn vectorTM
349 (Just $ AST.ConstraintIndex $ AST.IndexConstraint
350 [AST.ToRange (AST.PrimLit "0")
351 (AST.PrimName (AST.NAttribute $
352 AST.AttribName (AST.NSimple vecPar) (mkVHDLBasicId lengthId) Nothing) AST.:-:
353 (AST.PrimLit "1")) ]))
355 -- res AST.:= vec(0 to i-1) & a & vec(i+1 to length'vec-1)
356 replaceExpr = AST.NSimple resId AST.:=
357 (vecSlice (AST.PrimLit "0") (AST.PrimName (AST.NSimple iPar) AST.:-: AST.PrimLit "1") AST.:&:
358 AST.PrimName (AST.NSimple aPar) AST.:&:
359 vecSlice (AST.PrimName (AST.NSimple iPar) AST.:+: AST.PrimLit "1")
360 ((AST.PrimName (AST.NAttribute $
361 AST.AttribName (AST.NSimple vecPar) (mkVHDLBasicId lengthId) Nothing))
362 AST.:-: AST.PrimLit "1"))
363 replaceRet = AST.ReturnSm (Just $ AST.PrimName $ AST.NSimple resId)
364 vecSlice init last = AST.PrimName (AST.NSlice
367 (AST.ToRange init last)))
368 headSpec = AST.Function (mkVHDLExtId headId) [AST.IfaceVarDec vecPar vectorTM] elemTM
370 headExpr = AST.ReturnSm (Just $ (AST.PrimName $ AST.NIndexed (AST.IndexedName
371 (AST.NSimple vecPar) [AST.PrimLit "0"])))
372 lastSpec = AST.Function (mkVHDLExtId lastId) [AST.IfaceVarDec vecPar vectorTM] elemTM
373 -- return vec(vec'length-1);
374 lastExpr = AST.ReturnSm (Just $ (AST.PrimName $ AST.NIndexed (AST.IndexedName
376 [AST.PrimName (AST.NAttribute $
377 AST.AttribName (AST.NSimple vecPar) (mkVHDLBasicId lengthId) Nothing)
378 AST.:-: AST.PrimLit "1"])))
379 initSpec = AST.Function (mkVHDLExtId initId) [AST.IfaceVarDec vecPar vectorTM] vectorTM
380 -- variable res : fsvec_x (0 to vec'length-2);
383 (AST.SubtypeIn vectorTM
384 (Just $ AST.ConstraintIndex $ AST.IndexConstraint
385 [AST.ToRange (AST.PrimLit "0")
386 (AST.PrimName (AST.NAttribute $
387 AST.AttribName (AST.NSimple vecPar) (mkVHDLBasicId lengthId) Nothing) AST.:-:
388 (AST.PrimLit "2")) ]))
390 -- resAST.:= vec(0 to vec'length-2)
391 initExpr = AST.NSimple resId AST.:= (vecSlice
393 (AST.PrimName (AST.NAttribute $
394 AST.AttribName (AST.NSimple vecPar) (mkVHDLBasicId lengthId) Nothing)
395 AST.:-: AST.PrimLit "2"))
396 initRet = AST.ReturnSm (Just $ AST.PrimName $ AST.NSimple resId)
397 tailSpec = AST.Function (mkVHDLExtId tailId) [AST.IfaceVarDec vecPar vectorTM] vectorTM
398 -- variable res : fsvec_x (0 to vec'length-2);
401 (AST.SubtypeIn vectorTM
402 (Just $ AST.ConstraintIndex $ AST.IndexConstraint
403 [AST.ToRange (AST.PrimLit "0")
404 (AST.PrimName (AST.NAttribute $
405 AST.AttribName (AST.NSimple vecPar) (mkVHDLBasicId lengthId) Nothing) AST.:-:
406 (AST.PrimLit "2")) ]))
408 -- res AST.:= vec(1 to vec'length-1)
409 tailExpr = AST.NSimple resId AST.:= (vecSlice
411 (AST.PrimName (AST.NAttribute $
412 AST.AttribName (AST.NSimple vecPar) (mkVHDLBasicId lengthId) Nothing)
413 AST.:-: AST.PrimLit "1"))
414 tailRet = AST.ReturnSm (Just $ AST.PrimName $ AST.NSimple resId)
415 takeSpec = AST.Function (mkVHDLExtId takeId) [AST.IfaceVarDec nPar naturalTM,
416 AST.IfaceVarDec vecPar vectorTM ] vectorTM
417 -- variable res : fsvec_x (0 to n-1);
420 (AST.SubtypeIn vectorTM
421 (Just $ AST.ConstraintIndex $ AST.IndexConstraint
422 [AST.ToRange (AST.PrimLit "0")
423 ((AST.PrimName (AST.NSimple nPar)) AST.:-:
424 (AST.PrimLit "1")) ]))
426 -- res AST.:= vec(0 to n-1)
427 takeExpr = AST.NSimple resId AST.:=
428 (vecSlice (AST.PrimLit "1")
429 (AST.PrimName (AST.NSimple $ nPar) AST.:-: AST.PrimLit "1"))
430 takeRet = AST.ReturnSm (Just $ AST.PrimName $ AST.NSimple resId)
431 dropSpec = AST.Function (mkVHDLExtId dropId) [AST.IfaceVarDec nPar naturalTM,
432 AST.IfaceVarDec vecPar vectorTM ] vectorTM
433 -- variable res : fsvec_x (0 to vec'length-n-1);
436 (AST.SubtypeIn vectorTM
437 (Just $ AST.ConstraintIndex $ AST.IndexConstraint
438 [AST.ToRange (AST.PrimLit "0")
439 (AST.PrimName (AST.NAttribute $
440 AST.AttribName (AST.NSimple vecPar) (mkVHDLBasicId lengthId) Nothing) AST.:-:
441 (AST.PrimName $ AST.NSimple nPar)AST.:-: (AST.PrimLit "1")) ]))
443 -- res AST.:= vec(n to vec'length-1)
444 dropExpr = AST.NSimple resId AST.:= (vecSlice
445 (AST.PrimName $ AST.NSimple nPar)
446 (AST.PrimName (AST.NAttribute $
447 AST.AttribName (AST.NSimple vecPar) (mkVHDLBasicId lengthId) Nothing)
448 AST.:-: AST.PrimLit "1"))
449 dropRet = AST.ReturnSm (Just $ AST.PrimName $ AST.NSimple resId)
450 plusgtSpec = AST.Function (mkVHDLExtId plusgtId) [AST.IfaceVarDec aPar elemTM,
451 AST.IfaceVarDec vecPar vectorTM] vectorTM
452 -- variable res : fsvec_x (0 to vec'length);
455 (AST.SubtypeIn vectorTM
456 (Just $ AST.ConstraintIndex $ AST.IndexConstraint
457 [AST.ToRange (AST.PrimLit "0")
458 (AST.PrimName (AST.NAttribute $
459 AST.AttribName (AST.NSimple vecPar) (mkVHDLBasicId lengthId) Nothing))]))
461 plusgtExpr = AST.NSimple resId AST.:=
462 ((AST.PrimName $ AST.NSimple aPar) AST.:&:
463 (AST.PrimName $ AST.NSimple vecPar))
464 plusgtRet = AST.ReturnSm (Just $ AST.PrimName $ AST.NSimple resId)
465 emptySpec = AST.Function (mkVHDLExtId emptyId) [] vectorTM
468 (AST.SubtypeIn vectorTM Nothing)
469 (Just $ AST.PrimLit "\"\"")
470 emptyExpr = AST.ReturnSm (Just $ AST.PrimName (AST.NSimple resId))
471 singletonSpec = AST.Function (mkVHDLExtId singletonId) [AST.IfaceVarDec aPar elemTM ]
473 -- variable res : fsvec_x (0 to 0) := (others => a);
476 (AST.SubtypeIn vectorTM
477 (Just $ AST.ConstraintIndex $ AST.IndexConstraint
478 [AST.ToRange (AST.PrimLit "0") (AST.PrimLit "0")]))
479 (Just $ AST.Aggregate [AST.ElemAssoc (Just AST.Others)
480 (AST.PrimName $ AST.NSimple aPar)])
481 singletonRet = AST.ReturnSm (Just $ AST.PrimName $ AST.NSimple resId)
482 copySpec = AST.Function (mkVHDLExtId copyId) [AST.IfaceVarDec nPar naturalTM,
483 AST.IfaceVarDec aPar elemTM ] vectorTM
484 -- variable res : fsvec_x (0 to n-1) := (others => a);
487 (AST.SubtypeIn vectorTM
488 (Just $ AST.ConstraintIndex $ AST.IndexConstraint
489 [AST.ToRange (AST.PrimLit "0")
490 ((AST.PrimName (AST.NSimple nPar)) AST.:-:
491 (AST.PrimLit "1")) ]))
492 (Just $ AST.Aggregate [AST.ElemAssoc (Just AST.Others)
493 (AST.PrimName $ AST.NSimple aPar)])
495 copyExpr = AST.ReturnSm (Just $ AST.PrimName $ AST.NSimple resId)
496 selSpec = AST.Function (mkVHDLExtId selId) [AST.IfaceVarDec fPar naturalTM,
497 AST.IfaceVarDec sPar naturalTM,
498 AST.IfaceVarDec nPar naturalTM,
499 AST.IfaceVarDec vecPar vectorTM ] vectorTM
500 -- variable res : fsvec_x (0 to n-1);
503 (AST.SubtypeIn vectorTM
504 (Just $ AST.ConstraintIndex $ AST.IndexConstraint
505 [AST.ToRange (AST.PrimLit "0")
506 ((AST.PrimName (AST.NSimple nPar)) AST.:-:
507 (AST.PrimLit "1")) ])
510 -- for i res'range loop
511 -- res(i) := vec(f+i*s);
513 selFor = AST.ForSM iId (AST.AttribRange $ AST.AttribName (AST.NSimple resId) rangeId Nothing) [selAssign]
514 -- res(i) := vec(f+i*s);
515 selAssign = let origExp = AST.PrimName (AST.NSimple fPar) AST.:+:
516 (AST.PrimName (AST.NSimple iId) AST.:*:
517 AST.PrimName (AST.NSimple sPar)) in
518 AST.NIndexed (AST.IndexedName (AST.NSimple resId) [AST.PrimName (AST.NSimple iId)]) AST.:=
519 (AST.PrimName $ AST.NIndexed (AST.IndexedName (AST.NSimple vecPar) [origExp]))
521 selRet = AST.ReturnSm (Just $ AST.PrimName (AST.NSimple resId))
522 ltplusSpec = AST.Function (mkVHDLExtId ltplusId) [AST.IfaceVarDec vecPar vectorTM,
523 AST.IfaceVarDec aPar elemTM] vectorTM
524 -- variable res : fsvec_x (0 to vec'length);
527 (AST.SubtypeIn vectorTM
528 (Just $ AST.ConstraintIndex $ AST.IndexConstraint
529 [AST.ToRange (AST.PrimLit "0")
530 (AST.PrimName (AST.NAttribute $
531 AST.AttribName (AST.NSimple vecPar) (mkVHDLBasicId lengthId) Nothing))]))
533 ltplusExpr = AST.NSimple resId AST.:=
534 ((AST.PrimName $ AST.NSimple vecPar) AST.:&:
535 (AST.PrimName $ AST.NSimple aPar))
536 ltplusRet = AST.ReturnSm (Just $ AST.PrimName $ AST.NSimple resId)
537 plusplusSpec = AST.Function (mkVHDLExtId plusplusId) [AST.IfaceVarDec vec1Par vectorTM,
538 AST.IfaceVarDec vec2Par vectorTM]
540 -- variable res : fsvec_x (0 to vec1'length + vec2'length -1);
543 (AST.SubtypeIn vectorTM
544 (Just $ AST.ConstraintIndex $ AST.IndexConstraint
545 [AST.ToRange (AST.PrimLit "0")
546 (AST.PrimName (AST.NAttribute $
547 AST.AttribName (AST.NSimple vec1Par) (mkVHDLBasicId lengthId) Nothing) AST.:+:
548 AST.PrimName (AST.NAttribute $
549 AST.AttribName (AST.NSimple vec2Par) (mkVHDLBasicId lengthId) Nothing) AST.:-:
552 plusplusExpr = AST.NSimple resId AST.:=
553 ((AST.PrimName $ AST.NSimple vec1Par) AST.:&:
554 (AST.PrimName $ AST.NSimple vec2Par))
555 plusplusRet = AST.ReturnSm (Just $ AST.PrimName $ AST.NSimple resId)
557 -----------------------------------------------------------------------------
558 -- A table of builtin functions
559 -----------------------------------------------------------------------------
561 -- | The builtin functions we support. Maps a name to an argument count and a
563 globalNameTable :: NameTable
564 globalNameTable = Map.fromList
565 [ (exId , (2, genFCall ) )
566 , (replaceId , (3, genFCall ) )
567 , (headId , (1, genFCall ) )
568 , (lastId , (1, genFCall ) )
569 , (tailId , (1, genFCall ) )
570 , (initId , (1, genFCall ) )
571 , (takeId , (2, genFCall ) )
572 , (dropId , (2, genFCall ) )
573 , (selId , (4, genFCall ) )
574 , (plusgtId , (2, genFCall ) )
575 , (ltplusId , (2, genFCall ) )
576 , (plusplusId , (2, genFCall ) )
577 , (mapId , (2, genMap ) )
578 , (zipWithId , (3, genZipWith ) )
579 , (foldlId , (3, genFoldl ) )
580 , (foldrId , (3, genFoldr ) )
581 , (emptyId , (0, genFCall ) )
582 , (singletonId , (1, genFCall ) )
583 , (copyId , (2, genFCall ) )
584 , (hwxorId , (2, genOperator2 AST.Xor ) )
585 , (hwandId , (2, genOperator2 AST.And ) )
586 , (hworId , (2, genOperator2 AST.Or ) )
587 , (hwnotId , (1, genOperator1 AST.Not ) )