Add a anyset operation to HighOrdAlu, using foldl.
[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 = genVarArgs genFoldl'
137 genFoldl' :: (Either CoreSyn.CoreBndr AST.VHDLName) -> CoreSyn.CoreBndr -> [Var.Var] -> VHDLSession [AST.ConcSm]
138 genFoldl' (Left res) f [folded_f, start, vec] = do
139   -- evec is (TFVec n), so it still needs an element type
140   let (nvec, _) = splitAppTy (Var.varType vec)
141   -- Put the type of the start value in nvec, this will be the type of our
142   -- temporary vector
143   let tmp_ty = Type.mkAppTy nvec (Var.varType start)
144   tmp_vhdl_ty <- vhdl_ty tmp_ty
145   -- Setup the generate scheme
146   let gen_label = mkVHDLExtId ("foldlVector" ++ (varToString vec))
147   let block_label = mkVHDLExtId ("foldlVector" ++ (varToString start))
148   let gen_range = AST.ToRange (AST.PrimLit "0") len_min_expr
149   let gen_scheme   = AST.ForGn n_id gen_range
150   -- Make the intermediate vector
151   let  tmp_dec     = AST.BDISD $ AST.SigDec tmp_id tmp_vhdl_ty Nothing
152   -- Create the generate statement
153   cells <- sequence [genFirstCell, genOtherCell]
154   let gen_sm = AST.GenerateSm gen_label gen_scheme [] (map AST.CSGSm cells)
155   -- Assign tmp[len-1] to res
156   let out_assign = mkUncondAssign (Left res) $ vhdlNameToVHDLExpr (mkIndexedName tmp_name (AST.PrimLit $ show (len-1)))
157   let block = AST.BlockSm block_label [] (AST.PMapAspect []) [tmp_dec] [AST.CSGSm gen_sm, out_assign]
158   return [AST.CSBSm block]
159   where
160     -- The vector length
161     len         = (tfvec_len . Var.varType) vec
162     -- An id for the counter
163     n_id = mkVHDLBasicId "n"
164     n_expr = idToVHDLExpr n_id
165     -- An expression for n-1
166     n_min_expr = n_expr AST.:-: (AST.PrimLit "1")
167     -- An expression for len-1
168     len_min_expr = (AST.PrimLit $ show (len-1))
169     -- An id for the tmp result vector
170     tmp_id = mkVHDLBasicId "tmp"
171     tmp_name = AST.NSimple tmp_id
172     -- Generate parts of the fold
173     genFirstCell, genOtherCell :: VHDLSession AST.GenerateSm
174     genFirstCell = do
175       let cond_label = mkVHDLExtId "firstcell"
176       -- if n == 0
177       let cond_scheme = AST.IfGn $ n_expr AST.:=: (AST.PrimLit "0")
178       -- Output to tmp[n]
179       let resname = mkIndexedName tmp_name n_expr
180       -- Input from start
181       let argexpr1 = varToVHDLExpr start
182       -- Input from vec[n]
183       let argexpr2 = vhdlNameToVHDLExpr $ mkIndexedName (varToVHDLName vec) n_expr
184       app_concsms <- genApplication (Right resname) folded_f [Right argexpr1, Right argexpr2]
185       -- Return the conditional generate part
186       return $ AST.GenerateSm cond_label cond_scheme [] app_concsms
187
188     genOtherCell = do
189       let cond_label = mkVHDLExtId "othercell"
190       -- if n > 0
191       let cond_scheme = AST.IfGn $ n_expr AST.:>: (AST.PrimLit "0")
192       -- Output to tmp[n]
193       let resname = mkIndexedName tmp_name n_expr
194       -- Input from tmp[n-1]
195       let argexpr1 = vhdlNameToVHDLExpr $ mkIndexedName tmp_name n_min_expr
196       -- Input from vec[n]
197       let argexpr2 = vhdlNameToVHDLExpr $ mkIndexedName (varToVHDLName vec) n_expr
198       app_concsms <- genApplication (Right resname) folded_f [Right argexpr1, Right argexpr2]
199       -- Return the conditional generate part
200       return $ AST.GenerateSm cond_label cond_scheme [] app_concsms
201
202 -----------------------------------------------------------------------------
203 -- Function to generate VHDL for applications
204 -----------------------------------------------------------------------------
205 genApplication ::
206   (Either CoreSyn.CoreBndr AST.VHDLName) -- ^ Where to store the result?
207   -> CoreSyn.CoreBndr -- ^ The function to apply
208   -> [Either CoreSyn.CoreExpr AST.Expr] -- ^ The arguments to apply
209   -> VHDLSession [AST.ConcSm] -- ^ The resulting concurrent statements
210 genApplication dst f args =
211   case Var.globalIdVarDetails f of
212     IdInfo.DataConWorkId dc -> case dst of
213       -- It's a datacon. Create a record from its arguments.
214       Left bndr -> do
215         -- We have the bndr, so we can get at the type
216         labels <- getFieldLabels (Var.varType bndr)
217         return $ zipWith mkassign labels $ map (either exprToVHDLExpr id) args
218         where
219           mkassign :: AST.VHDLId -> AST.Expr -> AST.ConcSm
220           mkassign label arg =
221             let sel_name = mkSelectedName ((either varToVHDLName id) dst) label in
222             mkUncondAssign (Right sel_name) arg
223       Right _ -> error $ "Generate.genApplication Can't generate dataconstructor application without an original binder"
224     IdInfo.VanillaGlobal -> do
225       -- It's a global value imported from elsewhere. These can be builtin
226       -- functions. Look up the function name in the name table and execute
227       -- the associated builder if there is any and the argument count matches
228       -- (this should always be the case if it typechecks, but just to be
229       -- sure...).
230       case (Map.lookup (varToString f) globalNameTable) of
231         Just (arg_count, builder) ->
232           if length args == arg_count then
233             builder dst f args
234           else
235             error $ "Generate.genApplication Incorrect number of arguments to builtin function: " ++ pprString f ++ " Args: " ++ show args
236         Nothing -> error $ "Using function from another module that is not a known builtin: " ++ pprString f
237     IdInfo.NotGlobalId -> do
238       signatures <- getA vsSignatures
239       -- This is a local id, so it should be a function whose definition we
240       -- have and which can be turned into a component instantiation.
241       let  
242         signature = Maybe.fromMaybe 
243           (error $ "Using function '" ++ (varToString f) ++ "' without signature? This should not happen!") 
244           (Map.lookup f signatures)
245         entity_id = ent_id signature
246         -- TODO: Using show here isn't really pretty, but we'll need some
247         -- unique-ish value...
248         label = "comp_ins_" ++ (either show show) dst
249         portmaps = mkAssocElems (map (either exprToVHDLExpr id) args) ((either varToVHDLName id) dst) signature
250         in
251           return [mkComponentInst label entity_id portmaps]
252     details -> error $ "Calling unsupported function " ++ pprString f ++ " with GlobalIdDetails " ++ pprString details
253
254 -----------------------------------------------------------------------------
255 -- Functions to generate functions dealing with vectors.
256 -----------------------------------------------------------------------------
257
258 -- Returns the VHDLId of the vector function with the given name for the given
259 -- element type. Generates -- this function if needed.
260 vectorFunId :: Type.Type -> String -> VHDLSession AST.VHDLId
261 vectorFunId el_ty fname = do
262   elemTM <- vhdl_ty el_ty
263   -- TODO: This should not be duplicated from mk_vector_ty. Probably but it in
264   -- the VHDLState or something.
265   let vectorTM = mkVHDLExtId $ "vector_" ++ (AST.fromVHDLId elemTM)
266   typefuns <- getA vsTypeFuns
267   case Map.lookup (OrdType el_ty, fname) typefuns of
268     -- Function already generated, just return it
269     Just (id, _) -> return id
270     -- Function not generated yet, generate it
271     Nothing -> do
272       let functions = genUnconsVectorFuns elemTM vectorTM
273       case lookup fname functions of
274         Just body -> do
275           modA vsTypeFuns $ Map.insert (OrdType el_ty, fname) (function_id, body)
276           return function_id
277         Nothing -> error $ "I don't know how to generate vector function " ++ fname
278   where
279     function_id = mkVHDLExtId fname
280
281 genUnconsVectorFuns :: AST.TypeMark -- ^ type of the vector elements
282                     -> AST.TypeMark -- ^ type of the vector
283                     -> [(String, AST.SubProgBody)]
284 genUnconsVectorFuns elemTM vectorTM  = 
285   [ (exId, AST.SubProgBody exSpec      []                  [exExpr])
286   , (replaceId, AST.SubProgBody replaceSpec [AST.SPVD replaceVar] [replaceExpr,replaceRet])
287   , (headId, AST.SubProgBody headSpec    []                  [headExpr])
288   , (lastId, AST.SubProgBody lastSpec    []                  [lastExpr])
289   , (initId, AST.SubProgBody initSpec    [AST.SPVD initVar]  [initExpr, initRet])
290   , (tailId, AST.SubProgBody tailSpec    [AST.SPVD tailVar]  [tailExpr, tailRet])
291   , (takeId, AST.SubProgBody takeSpec    [AST.SPVD takeVar]  [takeExpr, takeRet])
292   , (dropId, AST.SubProgBody dropSpec    [AST.SPVD dropVar]  [dropExpr, dropRet])
293   , (plusgtId, AST.SubProgBody plusgtSpec  [AST.SPVD plusgtVar] [plusgtExpr, plusgtRet])
294   , (emptyId, AST.SubProgBody emptySpec   [AST.SPCD emptyVar] [emptyExpr])
295   , (singletonId, AST.SubProgBody singletonSpec [AST.SPVD singletonVar] [singletonRet])
296   , (copyId, AST.SubProgBody copySpec    [AST.SPVD copyVar]      [copyExpr])
297   ]
298   where 
299     ixPar   = AST.unsafeVHDLBasicId "ix"
300     vecPar  = AST.unsafeVHDLBasicId "vec"
301     nPar    = AST.unsafeVHDLBasicId "n"
302     iId     = AST.unsafeVHDLBasicId "i"
303     iPar    = iId
304     aPar    = AST.unsafeVHDLBasicId "a"
305     resId   = AST.unsafeVHDLBasicId "res"
306     exSpec = AST.Function (mkVHDLExtId exId) [AST.IfaceVarDec vecPar vectorTM,
307                                AST.IfaceVarDec ixPar  naturalTM] elemTM
308     exExpr = AST.ReturnSm (Just $ AST.PrimName $ AST.NIndexed 
309               (AST.IndexedName (AST.NSimple vecPar) [AST.PrimName $ 
310                 AST.NSimple ixPar]))
311     replaceSpec = AST.Function (mkVHDLExtId replaceId)  [ AST.IfaceVarDec vecPar vectorTM
312                                           , AST.IfaceVarDec iPar   naturalTM
313                                           , AST.IfaceVarDec aPar   elemTM
314                                           ] vectorTM 
315        -- variable res : fsvec_x (0 to vec'length-1);
316     replaceVar =
317          AST.VarDec resId 
318                 (AST.SubtypeIn vectorTM
319                   (Just $ AST.ConstraintIndex $ AST.IndexConstraint 
320                    [AST.ToRange (AST.PrimLit "0")
321                             (AST.PrimName (AST.NAttribute $ 
322                               AST.AttribName (AST.NSimple vecPar) (mkVHDLBasicId lengthId) Nothing) AST.:-:
323                                 (AST.PrimLit "1"))   ]))
324                 Nothing
325        --  res AST.:= vec(0 to i-1) & a & vec(i+1 to length'vec-1)
326     replaceExpr = AST.NSimple resId AST.:=
327            (vecSlice (AST.PrimLit "0") (AST.PrimName (AST.NSimple iPar) AST.:-: AST.PrimLit "1") AST.:&:
328             AST.PrimName (AST.NSimple aPar) AST.:&: 
329              vecSlice (AST.PrimName (AST.NSimple iPar) AST.:+: AST.PrimLit "1")
330                       ((AST.PrimName (AST.NAttribute $ 
331                                 AST.AttribName (AST.NSimple vecPar) (mkVHDLBasicId lengthId) Nothing)) 
332                                                               AST.:-: AST.PrimLit "1"))
333     replaceRet =  AST.ReturnSm (Just $ AST.PrimName $ AST.NSimple resId)
334     vecSlice init last =  AST.PrimName (AST.NSlice 
335                                         (AST.SliceName 
336                                               (AST.NSimple vecPar) 
337                                               (AST.ToRange init last)))
338     headSpec = AST.Function (mkVHDLExtId headId) [AST.IfaceVarDec vecPar vectorTM] elemTM
339        -- return vec(0);
340     headExpr = AST.ReturnSm (Just $ (AST.PrimName $ AST.NIndexed (AST.IndexedName 
341                     (AST.NSimple vecPar) [AST.PrimLit "0"])))
342     lastSpec = AST.Function (mkVHDLExtId lastId) [AST.IfaceVarDec vecPar vectorTM] elemTM
343        -- return vec(vec'length-1);
344     lastExpr = AST.ReturnSm (Just $ (AST.PrimName $ AST.NIndexed (AST.IndexedName 
345                     (AST.NSimple vecPar) 
346                     [AST.PrimName (AST.NAttribute $ 
347                                 AST.AttribName (AST.NSimple vecPar) (mkVHDLBasicId lengthId) Nothing) 
348                                                              AST.:-: AST.PrimLit "1"])))
349     initSpec = AST.Function (mkVHDLExtId initId) [AST.IfaceVarDec vecPar vectorTM] vectorTM 
350        -- variable res : fsvec_x (0 to vec'length-2);
351     initVar = 
352          AST.VarDec resId 
353                 (AST.SubtypeIn vectorTM
354                   (Just $ AST.ConstraintIndex $ AST.IndexConstraint 
355                    [AST.ToRange (AST.PrimLit "0")
356                             (AST.PrimName (AST.NAttribute $ 
357                               AST.AttribName (AST.NSimple vecPar) (mkVHDLBasicId lengthId) Nothing) AST.:-:
358                                 (AST.PrimLit "2"))   ]))
359                 Nothing
360        -- resAST.:= vec(0 to vec'length-2)
361     initExpr = AST.NSimple resId AST.:= (vecSlice 
362                                (AST.PrimLit "0") 
363                                (AST.PrimName (AST.NAttribute $ 
364                                   AST.AttribName (AST.NSimple vecPar) (mkVHDLBasicId lengthId) Nothing) 
365                                                              AST.:-: AST.PrimLit "2"))
366     initRet =  AST.ReturnSm (Just $ AST.PrimName $ AST.NSimple resId)
367     tailSpec = AST.Function (mkVHDLExtId tailId) [AST.IfaceVarDec vecPar vectorTM] vectorTM
368        -- variable res : fsvec_x (0 to vec'length-2); 
369     tailVar = 
370          AST.VarDec resId 
371                 (AST.SubtypeIn vectorTM
372                   (Just $ AST.ConstraintIndex $ AST.IndexConstraint 
373                    [AST.ToRange (AST.PrimLit "0")
374                             (AST.PrimName (AST.NAttribute $ 
375                               AST.AttribName (AST.NSimple vecPar) (mkVHDLBasicId lengthId) Nothing) AST.:-:
376                                 (AST.PrimLit "2"))   ]))
377                 Nothing       
378        -- res AST.:= vec(1 to vec'length-1)
379     tailExpr = AST.NSimple resId AST.:= (vecSlice 
380                                (AST.PrimLit "1") 
381                                (AST.PrimName (AST.NAttribute $ 
382                                   AST.AttribName (AST.NSimple vecPar) (mkVHDLBasicId lengthId) Nothing) 
383                                                              AST.:-: AST.PrimLit "1"))
384     tailRet = AST.ReturnSm (Just $ AST.PrimName $ AST.NSimple resId)
385     takeSpec = AST.Function (mkVHDLExtId takeId) [AST.IfaceVarDec nPar   naturalTM,
386                                    AST.IfaceVarDec vecPar vectorTM ] vectorTM
387        -- variable res : fsvec_x (0 to n-1);
388     takeVar = 
389          AST.VarDec resId 
390                 (AST.SubtypeIn vectorTM
391                   (Just $ AST.ConstraintIndex $ AST.IndexConstraint 
392                    [AST.ToRange (AST.PrimLit "0")
393                                ((AST.PrimName (AST.NSimple nPar)) AST.:-:
394                                 (AST.PrimLit "1"))   ]))
395                 Nothing
396        -- res AST.:= vec(0 to n-1)
397     takeExpr = AST.NSimple resId AST.:= 
398                     (vecSlice (AST.PrimLit "1") 
399                               (AST.PrimName (AST.NSimple $ nPar) AST.:-: AST.PrimLit "1"))
400     takeRet =  AST.ReturnSm (Just $ AST.PrimName $ AST.NSimple resId)
401     dropSpec = AST.Function (mkVHDLExtId dropId) [AST.IfaceVarDec nPar   naturalTM,
402                                    AST.IfaceVarDec vecPar vectorTM ] vectorTM 
403        -- variable res : fsvec_x (0 to vec'length-n-1);
404     dropVar = 
405          AST.VarDec resId 
406                 (AST.SubtypeIn vectorTM
407                   (Just $ AST.ConstraintIndex $ AST.IndexConstraint 
408                    [AST.ToRange (AST.PrimLit "0")
409                             (AST.PrimName (AST.NAttribute $ 
410                               AST.AttribName (AST.NSimple vecPar) (mkVHDLBasicId lengthId) Nothing) AST.:-:
411                                (AST.PrimName $ AST.NSimple nPar)AST.:-: (AST.PrimLit "1")) ]))
412                Nothing
413        -- res AST.:= vec(n to vec'length-1)
414     dropExpr = AST.NSimple resId AST.:= (vecSlice 
415                                (AST.PrimName $ AST.NSimple nPar) 
416                                (AST.PrimName (AST.NAttribute $ 
417                                   AST.AttribName (AST.NSimple vecPar) (mkVHDLBasicId lengthId) Nothing) 
418                                                              AST.:-: AST.PrimLit "1"))
419     dropRet =  AST.ReturnSm (Just $ AST.PrimName $ AST.NSimple resId)
420     plusgtSpec = AST.Function (mkVHDLExtId plusgtId) [AST.IfaceVarDec aPar   elemTM,
421                                        AST.IfaceVarDec vecPar vectorTM] vectorTM 
422     -- variable res : fsvec_x (0 to vec'length);
423     plusgtVar = 
424       AST.VarDec resId 
425              (AST.SubtypeIn vectorTM
426                (Just $ AST.ConstraintIndex $ AST.IndexConstraint 
427                 [AST.ToRange (AST.PrimLit "0")
428                         (AST.PrimName (AST.NAttribute $ 
429                           AST.AttribName (AST.NSimple vecPar) (mkVHDLBasicId lengthId) Nothing))]))
430              Nothing
431     plusgtExpr = AST.NSimple resId AST.:= 
432                    ((AST.PrimName $ AST.NSimple aPar) AST.:&: 
433                     (AST.PrimName $ AST.NSimple vecPar))
434     plusgtRet = AST.ReturnSm (Just $ AST.PrimName $ AST.NSimple resId)
435     emptySpec = AST.Function (mkVHDLExtId emptyId) [] vectorTM
436     emptyVar = 
437           AST.ConstDec resId 
438               (AST.SubtypeIn vectorTM Nothing)
439               (Just $ AST.PrimLit "\"\"")
440     emptyExpr = AST.ReturnSm (Just $ AST.PrimName (AST.NSimple resId))
441     singletonSpec = AST.Function (mkVHDLExtId singletonId) [AST.IfaceVarDec aPar elemTM ] 
442                                          vectorTM
443     -- variable res : fsvec_x (0 to 0) := (others => a);
444     singletonVar = 
445       AST.VarDec resId 
446              (AST.SubtypeIn vectorTM
447                (Just $ AST.ConstraintIndex $ AST.IndexConstraint 
448                 [AST.ToRange (AST.PrimLit "0") (AST.PrimLit "0")]))
449              (Just $ AST.Aggregate [AST.ElemAssoc (Just AST.Others) 
450                                           (AST.PrimName $ AST.NSimple aPar)])
451     singletonRet = AST.ReturnSm (Just $ AST.PrimName $ AST.NSimple resId)
452     copySpec = AST.Function (mkVHDLExtId copyId) [AST.IfaceVarDec nPar   naturalTM,
453                                    AST.IfaceVarDec aPar   elemTM   ] vectorTM 
454     -- variable res : fsvec_x (0 to n-1) := (others => a);
455     copyVar = 
456       AST.VarDec resId 
457              (AST.SubtypeIn vectorTM
458                (Just $ AST.ConstraintIndex $ AST.IndexConstraint 
459                 [AST.ToRange (AST.PrimLit "0")
460                             ((AST.PrimName (AST.NSimple nPar)) AST.:-:
461                              (AST.PrimLit "1"))   ]))
462              (Just $ AST.Aggregate [AST.ElemAssoc (Just AST.Others) 
463                                           (AST.PrimName $ AST.NSimple aPar)])
464     -- return res
465     copyExpr = AST.ReturnSm (Just $ AST.PrimName $ AST.NSimple resId)
466
467 -----------------------------------------------------------------------------
468 -- A table of builtin functions
469 -----------------------------------------------------------------------------
470
471 -- | The builtin functions we support. Maps a name to an argument count and a
472 -- builder function.
473 globalNameTable :: NameTable
474 globalNameTable = Map.fromList
475   [ (exId             , (2, genFCall                ) )
476   , (replaceId        , (3, genFCall                ) )
477   , (headId           , (1, genFCall                ) )
478   , (lastId           , (1, genFCall                ) )
479   , (tailId           , (1, genFCall                ) )
480   , (initId           , (1, genFCall                ) )
481   , (takeId           , (2, genFCall                ) )
482   , (dropId           , (2, genFCall                ) )
483   , (plusgtId         , (2, genFCall                ) )
484   , (mapId            , (2, genMap                  ) )
485   , (zipWithId        , (3, genZipWith              ) )
486   , (foldlId          , (3, genFoldl                ) )
487   , (emptyId          , (0, genFCall                ) )
488   , (singletonId      , (1, genFCall                ) )
489   , (copyId           , (2, genFCall                ) )
490   , (hwxorId          , (2, genOperator2 AST.Xor    ) )
491   , (hwandId          , (2, genOperator2 AST.And    ) )
492   , (hworId           , (2, genOperator2 AST.Or     ) )
493   , (hwnotId          , (1, genOperator1 AST.Not    ) )
494   ]