Parameterized fold, so that it can be used for foldl and foldr
[matthijs/master-project/cλash.git] / Generate.hs
1 module Generate where
2
3 -- Standard modules
4 import qualified Control.Monad as Monad
5 import qualified Data.Map as Map
6 import qualified Maybe
7 import qualified Data.Either as Either
8 import Data.Accessor
9 import Debug.Trace
10
11 -- ForSyDe
12 import qualified ForSyDe.Backend.VHDL.AST as AST
13
14 -- GHC API
15 import CoreSyn
16 import Type
17 import qualified Var
18 import qualified IdInfo
19
20 -- Local imports
21 import Constants
22 import VHDLTypes
23 import VHDLTools
24 import CoreTools
25 import Pretty
26
27 -----------------------------------------------------------------------------
28 -- Functions to generate VHDL for builtin functions
29 -----------------------------------------------------------------------------
30
31 -- | A function to wrap a builder-like function that expects its arguments to
32 -- be expressions.
33 genExprArgs ::
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
38   
39 -- | A function to wrap a builder-like function that expects its arguments to
40 -- be variables.
41 genVarArgs ::
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'
45   where
46     args' = map exprToVar exprargs
47     -- Check (rather crudely) that all arguments are CoreExprs
48     (exprargs, []) = Either.partitionEithers args
49
50 -- | A function to wrap a builder-like function that produces an expression
51 -- and expects it to be assigned to the destination.
52 genExprRes ::
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]
58
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
65
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
71
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
84
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] =
90   let
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
99
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
104   in do
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]
108
109 genMap' (Right name) _ _ = error $ "Cannot generate map function call assigned to a VHDLName: " ++ show name
110     
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] =
115   let
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
124
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
130   in do
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]
134
135 genFoldl :: BuiltinBuilder
136 genFoldl = genFold True
137
138 genFoldr :: BuiltinBuilder
139 genFoldr = genFold False
140
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
151   -- temporary vector
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]
171   where
172     -- The vector length
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
187     genFirstCell = do
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
194       -- Input from start
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]
200                                                                 else
201                                                                   [Right argexpr2, Right argexpr1]
202                                                               )
203       -- Return the conditional generate part
204       return $ AST.GenerateSm cond_label cond_scheme [] app_concsms
205
206     genOtherCell = do
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]
219                                                                 else
220                                                                   [Right argexpr2, Right argexpr1]
221                                                               )
222       -- Return the conditional generate part
223       return $ AST.GenerateSm cond_label cond_scheme [] app_concsms
224
225 -----------------------------------------------------------------------------
226 -- Function to generate VHDL for applications
227 -----------------------------------------------------------------------------
228 genApplication ::
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.
237       Left bndr -> do
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
241         where
242           mkassign :: AST.VHDLId -> AST.Expr -> AST.ConcSm
243           mkassign label arg =
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
252       -- sure...).
253       case (Map.lookup (varToString f) globalNameTable) of
254         Just (arg_count, builder) ->
255           if length args == arg_count then
256             builder dst f args
257           else
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.
264       let  
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
273         in
274           return [mkComponentInst label entity_id portmaps]
275     details -> error $ "Calling unsupported function " ++ pprString f ++ " with GlobalIdDetails " ++ pprString details
276
277 -----------------------------------------------------------------------------
278 -- Functions to generate functions dealing with vectors.
279 -----------------------------------------------------------------------------
280
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
294     Nothing -> do
295       let functions = genUnconsVectorFuns elemTM vectorTM
296       case lookup fname functions of
297         Just body -> do
298           modA vsTypeFuns $ Map.insert (OrdType el_ty, fname) (function_id, body)
299           return function_id
300         Nothing -> error $ "I don't know how to generate vector function " ++ fname
301   where
302     function_id = mkVHDLExtId fname
303
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   ]
321   where 
322     ixPar   = AST.unsafeVHDLBasicId "ix"
323     vecPar  = AST.unsafeVHDLBasicId "vec"
324     nPar    = AST.unsafeVHDLBasicId "n"
325     iId     = AST.unsafeVHDLBasicId "i"
326     iPar    = iId
327     aPar    = AST.unsafeVHDLBasicId "a"
328     resId   = AST.unsafeVHDLBasicId "res"
329     exSpec = AST.Function (mkVHDLExtId exId) [AST.IfaceVarDec vecPar vectorTM,
330                                AST.IfaceVarDec ixPar  naturalTM] elemTM
331     exExpr = AST.ReturnSm (Just $ AST.PrimName $ AST.NIndexed 
332               (AST.IndexedName (AST.NSimple vecPar) [AST.PrimName $ 
333                 AST.NSimple ixPar]))
334     replaceSpec = AST.Function (mkVHDLExtId replaceId)  [ AST.IfaceVarDec vecPar vectorTM
335                                           , AST.IfaceVarDec iPar   naturalTM
336                                           , AST.IfaceVarDec aPar   elemTM
337                                           ] vectorTM 
338        -- variable res : fsvec_x (0 to vec'length-1);
339     replaceVar =
340          AST.VarDec resId 
341                 (AST.SubtypeIn vectorTM
342                   (Just $ AST.ConstraintIndex $ AST.IndexConstraint 
343                    [AST.ToRange (AST.PrimLit "0")
344                             (AST.PrimName (AST.NAttribute $ 
345                               AST.AttribName (AST.NSimple vecPar) (mkVHDLBasicId lengthId) Nothing) AST.:-:
346                                 (AST.PrimLit "1"))   ]))
347                 Nothing
348        --  res AST.:= vec(0 to i-1) & a & vec(i+1 to length'vec-1)
349     replaceExpr = AST.NSimple resId AST.:=
350            (vecSlice (AST.PrimLit "0") (AST.PrimName (AST.NSimple iPar) AST.:-: AST.PrimLit "1") AST.:&:
351             AST.PrimName (AST.NSimple aPar) AST.:&: 
352              vecSlice (AST.PrimName (AST.NSimple iPar) AST.:+: AST.PrimLit "1")
353                       ((AST.PrimName (AST.NAttribute $ 
354                                 AST.AttribName (AST.NSimple vecPar) (mkVHDLBasicId lengthId) Nothing)) 
355                                                               AST.:-: AST.PrimLit "1"))
356     replaceRet =  AST.ReturnSm (Just $ AST.PrimName $ AST.NSimple resId)
357     vecSlice init last =  AST.PrimName (AST.NSlice 
358                                         (AST.SliceName 
359                                               (AST.NSimple vecPar) 
360                                               (AST.ToRange init last)))
361     headSpec = AST.Function (mkVHDLExtId headId) [AST.IfaceVarDec vecPar vectorTM] elemTM
362        -- return vec(0);
363     headExpr = AST.ReturnSm (Just $ (AST.PrimName $ AST.NIndexed (AST.IndexedName 
364                     (AST.NSimple vecPar) [AST.PrimLit "0"])))
365     lastSpec = AST.Function (mkVHDLExtId lastId) [AST.IfaceVarDec vecPar vectorTM] elemTM
366        -- return vec(vec'length-1);
367     lastExpr = AST.ReturnSm (Just $ (AST.PrimName $ AST.NIndexed (AST.IndexedName 
368                     (AST.NSimple vecPar) 
369                     [AST.PrimName (AST.NAttribute $ 
370                                 AST.AttribName (AST.NSimple vecPar) (mkVHDLBasicId lengthId) Nothing) 
371                                                              AST.:-: AST.PrimLit "1"])))
372     initSpec = AST.Function (mkVHDLExtId initId) [AST.IfaceVarDec vecPar vectorTM] vectorTM 
373        -- variable res : fsvec_x (0 to vec'length-2);
374     initVar = 
375          AST.VarDec resId 
376                 (AST.SubtypeIn vectorTM
377                   (Just $ AST.ConstraintIndex $ AST.IndexConstraint 
378                    [AST.ToRange (AST.PrimLit "0")
379                             (AST.PrimName (AST.NAttribute $ 
380                               AST.AttribName (AST.NSimple vecPar) (mkVHDLBasicId lengthId) Nothing) AST.:-:
381                                 (AST.PrimLit "2"))   ]))
382                 Nothing
383        -- resAST.:= vec(0 to vec'length-2)
384     initExpr = AST.NSimple resId AST.:= (vecSlice 
385                                (AST.PrimLit "0") 
386                                (AST.PrimName (AST.NAttribute $ 
387                                   AST.AttribName (AST.NSimple vecPar) (mkVHDLBasicId lengthId) Nothing) 
388                                                              AST.:-: AST.PrimLit "2"))
389     initRet =  AST.ReturnSm (Just $ AST.PrimName $ AST.NSimple resId)
390     tailSpec = AST.Function (mkVHDLExtId tailId) [AST.IfaceVarDec vecPar vectorTM] vectorTM
391        -- variable res : fsvec_x (0 to vec'length-2); 
392     tailVar = 
393          AST.VarDec resId 
394                 (AST.SubtypeIn vectorTM
395                   (Just $ AST.ConstraintIndex $ AST.IndexConstraint 
396                    [AST.ToRange (AST.PrimLit "0")
397                             (AST.PrimName (AST.NAttribute $ 
398                               AST.AttribName (AST.NSimple vecPar) (mkVHDLBasicId lengthId) Nothing) AST.:-:
399                                 (AST.PrimLit "2"))   ]))
400                 Nothing       
401        -- res AST.:= vec(1 to vec'length-1)
402     tailExpr = AST.NSimple resId AST.:= (vecSlice 
403                                (AST.PrimLit "1") 
404                                (AST.PrimName (AST.NAttribute $ 
405                                   AST.AttribName (AST.NSimple vecPar) (mkVHDLBasicId lengthId) Nothing) 
406                                                              AST.:-: AST.PrimLit "1"))
407     tailRet = AST.ReturnSm (Just $ AST.PrimName $ AST.NSimple resId)
408     takeSpec = AST.Function (mkVHDLExtId takeId) [AST.IfaceVarDec nPar   naturalTM,
409                                    AST.IfaceVarDec vecPar vectorTM ] vectorTM
410        -- variable res : fsvec_x (0 to n-1);
411     takeVar = 
412          AST.VarDec resId 
413                 (AST.SubtypeIn vectorTM
414                   (Just $ AST.ConstraintIndex $ AST.IndexConstraint 
415                    [AST.ToRange (AST.PrimLit "0")
416                                ((AST.PrimName (AST.NSimple nPar)) AST.:-:
417                                 (AST.PrimLit "1"))   ]))
418                 Nothing
419        -- res AST.:= vec(0 to n-1)
420     takeExpr = AST.NSimple resId AST.:= 
421                     (vecSlice (AST.PrimLit "1") 
422                               (AST.PrimName (AST.NSimple $ nPar) AST.:-: AST.PrimLit "1"))
423     takeRet =  AST.ReturnSm (Just $ AST.PrimName $ AST.NSimple resId)
424     dropSpec = AST.Function (mkVHDLExtId dropId) [AST.IfaceVarDec nPar   naturalTM,
425                                    AST.IfaceVarDec vecPar vectorTM ] vectorTM 
426        -- variable res : fsvec_x (0 to vec'length-n-1);
427     dropVar = 
428          AST.VarDec resId 
429                 (AST.SubtypeIn vectorTM
430                   (Just $ AST.ConstraintIndex $ AST.IndexConstraint 
431                    [AST.ToRange (AST.PrimLit "0")
432                             (AST.PrimName (AST.NAttribute $ 
433                               AST.AttribName (AST.NSimple vecPar) (mkVHDLBasicId lengthId) Nothing) AST.:-:
434                                (AST.PrimName $ AST.NSimple nPar)AST.:-: (AST.PrimLit "1")) ]))
435                Nothing
436        -- res AST.:= vec(n to vec'length-1)
437     dropExpr = AST.NSimple resId AST.:= (vecSlice 
438                                (AST.PrimName $ AST.NSimple nPar) 
439                                (AST.PrimName (AST.NAttribute $ 
440                                   AST.AttribName (AST.NSimple vecPar) (mkVHDLBasicId lengthId) Nothing) 
441                                                              AST.:-: AST.PrimLit "1"))
442     dropRet =  AST.ReturnSm (Just $ AST.PrimName $ AST.NSimple resId)
443     plusgtSpec = AST.Function (mkVHDLExtId plusgtId) [AST.IfaceVarDec aPar   elemTM,
444                                        AST.IfaceVarDec vecPar vectorTM] vectorTM 
445     -- variable res : fsvec_x (0 to vec'length);
446     plusgtVar = 
447       AST.VarDec resId 
448              (AST.SubtypeIn vectorTM
449                (Just $ AST.ConstraintIndex $ AST.IndexConstraint 
450                 [AST.ToRange (AST.PrimLit "0")
451                         (AST.PrimName (AST.NAttribute $ 
452                           AST.AttribName (AST.NSimple vecPar) (mkVHDLBasicId lengthId) Nothing))]))
453              Nothing
454     plusgtExpr = AST.NSimple resId AST.:= 
455                    ((AST.PrimName $ AST.NSimple aPar) AST.:&: 
456                     (AST.PrimName $ AST.NSimple vecPar))
457     plusgtRet = AST.ReturnSm (Just $ AST.PrimName $ AST.NSimple resId)
458     emptySpec = AST.Function (mkVHDLExtId emptyId) [] vectorTM
459     emptyVar = 
460           AST.ConstDec resId 
461               (AST.SubtypeIn vectorTM Nothing)
462               (Just $ AST.PrimLit "\"\"")
463     emptyExpr = AST.ReturnSm (Just $ AST.PrimName (AST.NSimple resId))
464     singletonSpec = AST.Function (mkVHDLExtId singletonId) [AST.IfaceVarDec aPar elemTM ] 
465                                          vectorTM
466     -- variable res : fsvec_x (0 to 0) := (others => a);
467     singletonVar = 
468       AST.VarDec resId 
469              (AST.SubtypeIn vectorTM
470                (Just $ AST.ConstraintIndex $ AST.IndexConstraint 
471                 [AST.ToRange (AST.PrimLit "0") (AST.PrimLit "0")]))
472              (Just $ AST.Aggregate [AST.ElemAssoc (Just AST.Others) 
473                                           (AST.PrimName $ AST.NSimple aPar)])
474     singletonRet = AST.ReturnSm (Just $ AST.PrimName $ AST.NSimple resId)
475     copySpec = AST.Function (mkVHDLExtId copyId) [AST.IfaceVarDec nPar   naturalTM,
476                                    AST.IfaceVarDec aPar   elemTM   ] vectorTM 
477     -- variable res : fsvec_x (0 to n-1) := (others => a);
478     copyVar = 
479       AST.VarDec resId 
480              (AST.SubtypeIn vectorTM
481                (Just $ AST.ConstraintIndex $ AST.IndexConstraint 
482                 [AST.ToRange (AST.PrimLit "0")
483                             ((AST.PrimName (AST.NSimple nPar)) AST.:-:
484                              (AST.PrimLit "1"))   ]))
485              (Just $ AST.Aggregate [AST.ElemAssoc (Just AST.Others) 
486                                           (AST.PrimName $ AST.NSimple aPar)])
487     -- return res
488     copyExpr = AST.ReturnSm (Just $ AST.PrimName $ AST.NSimple resId)
489
490 -----------------------------------------------------------------------------
491 -- A table of builtin functions
492 -----------------------------------------------------------------------------
493
494 -- | The builtin functions we support. Maps a name to an argument count and a
495 -- builder function.
496 globalNameTable :: NameTable
497 globalNameTable = Map.fromList
498   [ (exId             , (2, genFCall                ) )
499   , (replaceId        , (3, genFCall                ) )
500   , (headId           , (1, genFCall                ) )
501   , (lastId           , (1, genFCall                ) )
502   , (tailId           , (1, genFCall                ) )
503   , (initId           , (1, genFCall                ) )
504   , (takeId           , (2, genFCall                ) )
505   , (dropId           , (2, genFCall                ) )
506   , (plusgtId         , (2, genFCall                ) )
507   , (mapId            , (2, genMap                  ) )
508   , (zipWithId        , (3, genZipWith              ) )
509   , (foldlId          , (3, genFoldl                ) )
510   , (foldrId          , (3, genFoldr                ) )
511   , (emptyId          , (0, genFCall                ) )
512   , (singletonId      , (1, genFCall                ) )
513   , (copyId           , (2, genFCall                ) )
514   , (hwxorId          , (2, genOperator2 AST.Xor    ) )
515   , (hwandId          , (2, genOperator2 AST.And    ) )
516   , (hworId           , (2, genOperator2 AST.Or     ) )
517   , (hwnotId          , (1, genOperator1 AST.Not    ) )
518   ]