Ignore all applications that have an empty result type.
[matthijs/master-project/cλash.git] / cλash / CLasH / VHDL / Generate.hs
1 module CLasH.VHDL.Generate where
2
3 -- Standard modules
4 import qualified Data.List as List
5 import qualified Data.Map as Map
6 import qualified Control.Monad as Monad
7 import qualified Maybe
8 import qualified Data.Either as Either
9 import qualified Data.Accessor.Monad.Trans.State as MonadState
10
11 -- VHDL Imports
12 import qualified Language.VHDL.AST as AST
13
14 -- GHC API
15 import qualified CoreSyn
16 import qualified Type
17 import qualified Var
18 import qualified Id
19 import qualified IdInfo
20 import qualified Literal
21 import qualified Name
22 import qualified TyCon
23
24 -- Local imports
25 import CLasH.Translator.TranslatorTypes
26 import CLasH.VHDL.Constants
27 import CLasH.VHDL.VHDLTypes
28 import CLasH.VHDL.VHDLTools
29 import CLasH.Utils
30 import CLasH.Utils.Core.CoreTools
31 import CLasH.Utils.Pretty
32 import qualified CLasH.Normalize as Normalize
33
34 -----------------------------------------------------------------------------
35 -- Functions to generate VHDL for user-defined functions.
36 -----------------------------------------------------------------------------
37
38 -- | Create an entity for a given function
39 getEntity ::
40   CoreSyn.CoreBndr
41   -> TranslatorSession Entity -- ^ The resulting entity
42
43 getEntity fname = makeCached fname tsEntities $ do
44       expr <- Normalize.getNormalized fname
45       -- Split the normalized expression
46       let (args, binds, res) = Normalize.splitNormalized expr
47       -- Generate ports for all non-empty types
48       args' <- catMaybesM $ mapM mkMap args
49       -- TODO: Handle Nothing
50       res' <- mkMap res
51       count <- MonadState.get tsEntityCounter 
52       let vhdl_id = mkVHDLBasicId $ varToString fname ++ "Component_" ++ show count
53       MonadState.set tsEntityCounter (count + 1)
54       let ent_decl = createEntityAST vhdl_id args' res'
55       let signature = Entity vhdl_id args' res' ent_decl
56       return signature
57   where
58     mkMap ::
59       --[(SignalId, SignalInfo)] 
60       CoreSyn.CoreBndr 
61       -> TranslatorSession (Maybe Port)
62     mkMap = (\bndr ->
63       let
64         --info = Maybe.fromMaybe
65         --  (error $ "Signal not found in the name map? This should not happen!")
66         --  (lookup id sigmap)
67         --  Assume the bndr has a valid VHDL id already
68         id = varToVHDLId bndr
69         ty = Var.varType bndr
70         error_msg = "\nVHDL.createEntity.mkMap: Can not create entity: " ++ pprString fname ++ "\nbecause no type can be created for port: " ++ pprString bndr 
71       in do
72         type_mark_maybe <- MonadState.lift tsType $ vhdlTy error_msg ty
73         case type_mark_maybe of 
74           Just type_mark -> return $ Just (id, type_mark)
75           Nothing -> return Nothing
76      )
77
78 -- | Create the VHDL AST for an entity
79 createEntityAST ::
80   AST.VHDLId                   -- ^ The name of the function
81   -> [Port]                    -- ^ The entity's arguments
82   -> Maybe Port                -- ^ The entity's result
83   -> AST.EntityDec             -- ^ The entity with the ent_decl filled in as well
84
85 createEntityAST vhdl_id args res =
86   AST.EntityDec vhdl_id ports
87   where
88     -- Create a basic Id, since VHDL doesn't grok filenames with extended Ids.
89     ports = map (mkIfaceSigDec AST.In) args
90               ++ (Maybe.maybeToList res_port)
91               ++ [clk_port,resetn_port]
92     -- Add a clk port if we have state
93     clk_port = AST.IfaceSigDec clockId AST.In std_logicTM
94     resetn_port = AST.IfaceSigDec resetId AST.In std_logicTM
95     res_port = fmap (mkIfaceSigDec AST.Out) res
96
97 -- | Create a port declaration
98 mkIfaceSigDec ::
99   AST.Mode                         -- ^ The mode for the port (In / Out)
100   -> Port                          -- ^ The id and type for the port
101   -> AST.IfaceSigDec               -- ^ The resulting port declaration
102
103 mkIfaceSigDec mode (id, ty) = AST.IfaceSigDec id mode ty
104
105 -- | Create an architecture for a given function
106 getArchitecture ::
107   CoreSyn.CoreBndr -- ^ The function to get an architecture for
108   -> TranslatorSession (Architecture, [CoreSyn.CoreBndr])
109   -- ^ The architecture for this function
110
111 getArchitecture fname = makeCached fname tsArchitectures $ do
112   expr <- Normalize.getNormalized fname
113   -- Split the normalized expression
114   let (args, binds, res) = Normalize.splitNormalized expr
115   
116   -- Get the entity for this function
117   signature <- getEntity fname
118   let entity_id = ent_id signature
119
120   -- Create signal declarations for all binders in the let expression, except
121   -- for the output port (that will already have an output port declared in
122   -- the entity).
123   sig_dec_maybes <- mapM (mkSigDec . fst) (filter ((/=res).fst) binds)
124   let sig_decs = Maybe.catMaybes sig_dec_maybes
125   -- Process each bind, resulting in info about state variables and concurrent
126   -- statements.
127   (state_vars, sms) <- Monad.mapAndUnzipM dobind binds
128   let (in_state_maybes, out_state_maybes) = unzip state_vars
129   let (statementss, used_entitiess) = unzip sms
130   -- Get initial state, if it's there
131   initSmap <- MonadState.get tsInitStates
132   let init_state = Map.lookup fname initSmap
133   -- Create a state proc, if needed
134   (state_proc, resbndr) <- case (Maybe.catMaybes in_state_maybes, Maybe.catMaybes out_state_maybes, init_state) of
135         ([in_state], [out_state], Nothing) -> do 
136           nonEmpty <- hasNonEmptyType in_state
137           if nonEmpty then error ("No initial state defined for: " ++ show fname) else return ([],[])
138         ([in_state], [out_state], Just resetval) -> mkStateProcSm (in_state, out_state,resetval)
139         ([], [], Just _) -> error $ "Initial state defined for state-less function: " ++ show fname
140         ([], [], Nothing) -> return ([],[])
141         (ins, outs, res) -> error $ "Weird use of state in " ++ show fname ++ ". In: " ++ show ins ++ " Out: " ++ show outs
142   -- Join the create statements and the (optional) state_proc
143   let statements = concat statementss ++ state_proc
144   -- Create the architecture
145   let arch = AST.ArchBody (mkVHDLBasicId "structural") (AST.NSimple entity_id) (map AST.BDISD sig_decs) statements
146   let used_entities = (concat used_entitiess) ++ resbndr
147   return (arch, used_entities)
148   where
149     dobind :: (CoreSyn.CoreBndr, CoreSyn.CoreExpr) -- ^ The bind to process
150               -> TranslatorSession ((Maybe CoreSyn.CoreBndr, Maybe CoreSyn.CoreBndr), ([AST.ConcSm], [CoreSyn.CoreBndr]))
151               -- ^ ((Input state variable, output state variable), (statements, used entities))
152     -- newtype unpacking is just a cast
153     dobind (bndr, unpacked@(CoreSyn.Cast packed coercion)) 
154       | hasStateType packed && not (hasStateType unpacked)
155       = return ((Just bndr, Nothing), ([], []))
156     -- With simplCore, newtype packing is just a cast
157     dobind (bndr, packed@(CoreSyn.Cast unpacked@(CoreSyn.Var state) coercion)) 
158       | hasStateType packed && not (hasStateType unpacked)
159       = return ((Nothing, Just state), ([], []))
160     -- Without simplCore, newtype packing uses a data constructor
161     dobind (bndr, (CoreSyn.App (CoreSyn.App (CoreSyn.Var con) (CoreSyn.Type _)) (CoreSyn.Var state))) 
162       | isStateCon con
163       = return ((Nothing, Just state), ([], []))
164     -- Anything else is handled by mkConcSm
165     dobind bind = do
166       sms <- mkConcSm bind
167       return ((Nothing, Nothing), sms)
168
169 mkStateProcSm :: 
170   (CoreSyn.CoreBndr, CoreSyn.CoreBndr, CoreSyn.CoreBndr) -- ^ The current state, new state and reset variables
171   -> TranslatorSession ([AST.ConcSm], [CoreSyn.CoreBndr]) -- ^ The resulting statements
172 mkStateProcSm (old, new, res) = do
173   let error_msg = "\nVHDL.mkSigDec: Can not make signal declaration for type: \n" ++ pprString res 
174   type_mark_old_maybe <- MonadState.lift tsType $ vhdlTy error_msg (Var.varType old)
175   let type_mark_old = Maybe.fromJust type_mark_old_maybe
176   type_mark_res_maybe <- MonadState.lift tsType $ vhdlTy error_msg (Var.varType res)
177   let type_mark_res' = Maybe.fromJust type_mark_res_maybe
178   let type_mark_res = if type_mark_old == type_mark_res' then
179                         type_mark_res'
180                       else 
181                         error $ "Initial state has different type than state type, state type: " ++ show type_mark_old ++ ", init type: "  ++ show type_mark_res'    
182   let resvalid  = mkVHDLExtId $ varToString res ++ "val"
183   let resvaldec = AST.BDISD $ AST.SigDec resvalid type_mark_res Nothing
184   let reswform  = AST.Wform [AST.WformElem (AST.PrimName $ AST.NSimple resvalid) Nothing]
185   let res_assign = AST.SigAssign (varToVHDLName old) reswform
186   let blocklabel       = mkVHDLBasicId "state"
187   let statelabel  = mkVHDLBasicId "stateupdate"
188   let rising_edge = AST.NSimple $ mkVHDLBasicId "rising_edge"
189   let wform       = AST.Wform [AST.WformElem (AST.PrimName $ varToVHDLName new) Nothing]
190   let clk_assign      = AST.SigAssign (varToVHDLName old) wform
191   let rising_edge_clk = AST.PrimFCall $ AST.FCall rising_edge [Nothing AST.:=>: (AST.ADName $ AST.NSimple clockId)]
192   let resetn_is_low  = (AST.PrimName $ AST.NSimple resetId) AST.:=: (AST.PrimLit "'0'")
193   signature <- getEntity res
194   let entity_id = ent_id signature
195   let reslabel = "resetval_" ++ ((prettyShow . varToVHDLName) res)
196   let portmaps = mkAssocElems [] (AST.NSimple resvalid) signature
197   let reset_statement = mkComponentInst reslabel entity_id portmaps
198   let clk_statement = [AST.ElseIf rising_edge_clk [clk_assign]]
199   let statement   = AST.IfSm resetn_is_low [res_assign] clk_statement Nothing
200   let stateupdate = AST.CSPSm $ AST.ProcSm statelabel [clockId,resetId,resvalid] [statement]
201   let block = AST.CSBSm $ AST.BlockSm blocklabel [] (AST.PMapAspect []) [resvaldec] [reset_statement,stateupdate]
202   return ([block],[res])
203
204 -- | Transforms a core binding into a VHDL concurrent statement
205 mkConcSm ::
206   (CoreSyn.CoreBndr, CoreSyn.CoreExpr) -- ^ The binding to process
207   -> TranslatorSession ([AST.ConcSm], [CoreSyn.CoreBndr]) 
208   -- ^ The corresponding VHDL concurrent statements and entities
209   --   instantiated.
210
211
212 -- Ignore Cast expressions, they should not longer have any meaning as long as
213 -- the type works out. Throw away state repacking
214 mkConcSm (bndr, to@(CoreSyn.Cast from ty))
215   | hasStateType to && hasStateType from
216   = return ([],[])
217 mkConcSm (bndr, CoreSyn.Cast expr ty) = mkConcSm (bndr, expr)
218
219 -- Simple a = b assignments are just like applications, but without arguments.
220 -- We can't just generate an unconditional assignment here, since b might be a
221 -- top level binding (e.g., a function with no arguments).
222 mkConcSm (bndr, CoreSyn.Var v) =
223   genApplication (Left bndr) v []
224
225 mkConcSm (bndr, app@(CoreSyn.App _ _))= do
226   let (CoreSyn.Var f, args) = CoreSyn.collectArgs app
227   let valargs = get_val_args (Var.varType f) args
228   genApplication (Left bndr) f (map Left valargs)
229
230 -- A single alt case must be a selector. This means the scrutinee is a simple
231 -- variable, the alternative is a dataalt with a single non-wild binder that
232 -- is also returned.
233 mkConcSm (bndr, expr@(CoreSyn.Case (CoreSyn.Var scrut) b ty [alt])) 
234                 -- Don't generate VHDL for substate extraction
235                 | hasStateType bndr = return ([], [])
236                 | otherwise =
237   case alt of
238     (CoreSyn.DataAlt dc, bndrs, (CoreSyn.Var sel_bndr)) -> do
239       nonemptysel <- hasNonEmptyType sel_bndr 
240       if nonemptysel 
241         then do
242           bndrs' <- Monad.filterM hasNonEmptyType bndrs
243           case List.elemIndex sel_bndr bndrs' of
244             Just i -> do
245               htypeScrt <- MonadState.lift tsType $ mkHTypeEither (Var.varType scrut)
246               htypeBndr <- MonadState.lift tsType $ mkHTypeEither (Var.varType bndr)
247               case htypeScrt == htypeBndr of
248                 True -> do
249                   let sel_name = varToVHDLName scrut
250                   let sel_expr = AST.PrimName sel_name
251                   return ([mkUncondAssign (Left bndr) sel_expr], [])
252                 otherwise ->
253                   case htypeScrt of
254                     Right (AggrType _ _) -> do
255                       labels <- MonadState.lift tsType $ getFieldLabels (Id.idType scrut)
256                       let label = labels!!i
257                       let sel_name = mkSelectedName (varToVHDLName scrut) label
258                       let sel_expr = AST.PrimName sel_name
259                       return ([mkUncondAssign (Left bndr) sel_expr], [])
260                     _ -> do -- error $ "DIE!"
261                       let sel_name = varToVHDLName scrut
262                       let sel_expr = AST.PrimName sel_name
263                       return ([mkUncondAssign (Left bndr) sel_expr], [])
264             Nothing -> error $ "\nVHDL.mkConcSM: Not in normal form: Not a selector case: result is not one of the binders\n" ++ (pprString expr)
265           else
266             -- A selector case that selects a state value, ignore it.
267             return ([], [])
268       
269     _ -> error $ "\nVHDL.mkConcSM: Not in normal form: Not a selector case:\n" ++ (pprString expr)
270
271 -- Multiple case alt are be conditional assignments and have only wild
272 -- binders in the alts and only variables in the case values and a variable
273 -- for a scrutinee. We check the constructor of the second alt, since the
274 -- first is the default case, if there is any.
275
276 -- mkConcSm (bndr, (CoreSyn.Case (CoreSyn.Var scrut) b ty [(_, _, CoreSyn.Var false), (con, _, CoreSyn.Var true)])) = do
277 --   scrut' <- MonadState.lift tsType $ varToVHDLExpr scrut
278 --   altcon <- MonadState.lift tsType $ altconToVHDLExpr con
279 --   let cond_expr = scrut' AST.:=: altcon
280 --   true_expr <- MonadState.lift tsType $ varToVHDLExpr true
281 --   false_expr <- MonadState.lift tsType $ varToVHDLExpr false
282 --   return ([mkCondAssign (Left bndr) cond_expr true_expr false_expr], [])
283 mkConcSm (bndr, (CoreSyn.Case (CoreSyn.Var scrut) _ _ (alt:alts))) = do --error "\nVHDL.mkConcSm: Not in normal form: Case statement with more than two alternatives"
284   scrut' <- MonadState.lift tsType $ varToVHDLExpr scrut
285   -- Omit first condition, which is the default
286   altcons <- MonadState.lift tsType $ mapM (altconToVHDLExpr . (\(con,_,_) -> con)) alts
287   let cond_exprs = map (\x -> scrut' AST.:=: x) altcons
288   -- Rotate expressions to the left, so that the expression related to the default case is the last
289   exprs <- MonadState.lift tsType $ mapM (varToVHDLExpr . (\(_,_,CoreSyn.Var expr) -> expr)) (alts ++ [alt])
290   return ([mkAltsAssign (Left bndr) cond_exprs exprs], [])
291
292 mkConcSm (_, CoreSyn.Case _ _ _ _) = error "\nVHDL.mkConcSm: Not in normal form: Case statement has does not have a simple variable as scrutinee"
293 mkConcSm (bndr, expr) = error $ "\nVHDL.mkConcSM: Unsupported binding in let expression: " ++ pprString bndr ++ " = " ++ pprString expr
294
295 -----------------------------------------------------------------------------
296 -- Functions to generate VHDL for builtin functions
297 -----------------------------------------------------------------------------
298
299 -- | A function to wrap a builder-like function that expects its arguments to
300 -- be expressions.
301 genExprArgs wrap dst func args = do
302   args' <- argsToVHDLExprs args
303   wrap dst func args'
304
305 -- | Turn the all lefts into VHDL Expressions.
306 argsToVHDLExprs :: [Either CoreSyn.CoreExpr AST.Expr] -> TranslatorSession [AST.Expr]
307 argsToVHDLExprs = catMaybesM . (mapM argToVHDLExpr)
308
309 argToVHDLExpr :: Either CoreSyn.CoreExpr AST.Expr -> TranslatorSession (Maybe AST.Expr)
310 argToVHDLExpr (Left expr) = MonadState.lift tsType $ do
311   let errmsg = "Generate.argToVHDLExpr: Using non-representable type? Should not happen!"
312   ty_maybe <- vhdlTy errmsg expr
313   case ty_maybe of
314     Just _ -> do
315       vhdl_expr <- varToVHDLExpr $ exprToVar expr
316       return $ Just vhdl_expr
317     Nothing -> return Nothing
318
319 argToVHDLExpr (Right expr) = return $ Just expr
320
321 -- A function to wrap a builder-like function that generates no component
322 -- instantiations
323 genNoInsts ::
324   (dst -> func -> args -> TranslatorSession [AST.ConcSm])
325   -> (dst -> func -> args -> TranslatorSession ([AST.ConcSm], [CoreSyn.CoreBndr]))
326 genNoInsts wrap dst func args = do
327   concsms <- wrap dst func args
328   return (concsms, [])
329
330 -- | A function to wrap a builder-like function that expects its arguments to
331 -- be variables.
332 genVarArgs ::
333   (dst -> func -> [Var.Var] -> res)
334   -> (dst -> func -> [Either CoreSyn.CoreExpr AST.Expr] -> res)
335 genVarArgs wrap dst func args = wrap dst func args'
336   where
337     args' = map exprToVar exprargs
338     -- Check (rather crudely) that all arguments are CoreExprs
339     (exprargs, []) = Either.partitionEithers args
340
341 -- | A function to wrap a builder-like function that expects its arguments to
342 -- be Literals
343 genLitArgs ::
344   (dst -> func -> [Literal.Literal] -> TranslatorSession [AST.ConcSm])
345   -> (dst -> func -> [Either CoreSyn.CoreExpr AST.Expr] -> TranslatorSession [AST.ConcSm])
346 genLitArgs wrap dst func args = do
347   hscenv <- MonadState.lift tsType $ MonadState.get tsHscEnv
348   let (exprargs, []) = Either.partitionEithers args
349   -- FIXME: Check if we were passed an CoreSyn.App
350   let litargs = concatMap (getLiterals hscenv) exprargs
351   let args' = map exprToLit litargs
352   wrap dst func args'   
353
354 -- | A function to wrap a builder-like function that produces an expression
355 -- and expects it to be assigned to the destination.
356 genExprRes ::
357   ((Either CoreSyn.CoreBndr AST.VHDLName) -> func -> [arg] -> TranslatorSession AST.Expr)
358   -> ((Either CoreSyn.CoreBndr AST.VHDLName) -> func -> [arg] -> TranslatorSession [AST.ConcSm])
359 genExprRes wrap dst func args = do
360   expr <- wrap dst func args
361   return [mkUncondAssign dst expr]
362
363 -- | Generate a binary operator application. The first argument should be a
364 -- constructor from the AST.Expr type, e.g. AST.And.
365 genOperator2 :: (AST.Expr -> AST.Expr -> AST.Expr) -> BuiltinBuilder 
366 genOperator2 op = genNoInsts $ genExprArgs $ genExprRes (genOperator2' op)
367 genOperator2' :: (AST.Expr -> AST.Expr -> AST.Expr) -> dst -> CoreSyn.CoreBndr -> [AST.Expr] -> TranslatorSession AST.Expr
368 genOperator2' op _ f [arg1, arg2] = return $ op arg1 arg2
369
370 -- | Generate a unary operator application
371 genOperator1 :: (AST.Expr -> AST.Expr) -> BuiltinBuilder 
372 genOperator1 op = genNoInsts $ genExprArgs $ genExprRes (genOperator1' op)
373 genOperator1' :: (AST.Expr -> AST.Expr) -> dst -> CoreSyn.CoreBndr -> [AST.Expr] -> TranslatorSession AST.Expr
374 genOperator1' op _ f [arg] = return $ op arg
375
376 -- | Generate a unary operator application
377 genNegation :: BuiltinBuilder 
378 genNegation = genNoInsts $ genVarArgs $ genExprRes genNegation'
379 genNegation' :: dst -> CoreSyn.CoreBndr -> [Var.Var] -> TranslatorSession AST.Expr
380 genNegation' _ f [arg] = do
381   arg1 <- MonadState.lift tsType $ varToVHDLExpr arg
382   let ty = Var.varType arg
383   let (tycon, args) = Type.splitTyConApp ty
384   let name = Name.getOccString (TyCon.tyConName tycon)
385   case name of
386     "SizedInt" -> return $ AST.Neg arg1
387     otherwise -> error $ "\nGenerate.genNegation': Negation not allowed for type: " ++ show name 
388
389 -- | Generate a function call from the destination binder, function name and a
390 -- list of expressions (its arguments)
391 genFCall :: Bool -> BuiltinBuilder 
392 genFCall switch = genNoInsts $ genExprArgs $ genExprRes (genFCall' switch)
393 genFCall' :: Bool -> Either CoreSyn.CoreBndr AST.VHDLName -> CoreSyn.CoreBndr -> [AST.Expr] -> TranslatorSession AST.Expr
394 genFCall' switch (Left res) f args = do
395   let fname = varToString f
396   let el_ty = if switch then (Var.varType res) else ((tfvec_elem . Var.varType) res)
397   id <- MonadState.lift tsType $ vectorFunId el_ty fname
398   return $ AST.PrimFCall $ AST.FCall (AST.NSimple id)  $
399              map (\exp -> Nothing AST.:=>: AST.ADExpr exp) args
400 genFCall' _ (Right name) _ _ = error $ "\nGenerate.genFCall': Cannot generate builtin function call assigned to a VHDLName: " ++ show name
401
402 genFromSizedWord :: BuiltinBuilder
403 genFromSizedWord = genNoInsts $ genExprArgs genFromSizedWord'
404 genFromSizedWord' :: Either CoreSyn.CoreBndr AST.VHDLName -> CoreSyn.CoreBndr -> [AST.Expr] -> TranslatorSession [AST.ConcSm]
405 genFromSizedWord' (Left res) f args@[arg] =
406   return [mkUncondAssign (Left res) arg]
407   -- let fname = varToString f
408   -- return $ AST.PrimFCall $ AST.FCall (AST.NSimple (mkVHDLBasicId toIntegerId))  $
409   --            map (\exp -> Nothing AST.:=>: AST.ADExpr exp) args
410 genFromSizedWord' (Right name) _ _ = error $ "\nGenerate.genFromSizedWord': Cannot generate builtin function call assigned to a VHDLName: " ++ show name
411
412 genResize :: BuiltinBuilder
413 genResize = genNoInsts $ genExprArgs $ genExprRes genResize'
414 genResize' :: Either CoreSyn.CoreBndr AST.VHDLName -> CoreSyn.CoreBndr -> [AST.Expr] -> TranslatorSession AST.Expr
415 genResize' (Left res) f [arg] = do {
416   ; let { ty = Var.varType res
417         ; (tycon, args) = Type.splitTyConApp ty
418         ; name = Name.getOccString (TyCon.tyConName tycon)
419         } ;
420   ; len <- case name of
421       "SizedInt" -> MonadState.lift tsType $ tfp_to_int (sized_int_len_ty ty)
422       "SizedWord" -> MonadState.lift tsType $ tfp_to_int (sized_word_len_ty ty)
423   ; return $ AST.PrimFCall $ AST.FCall (AST.NSimple (mkVHDLBasicId resizeId))
424              [Nothing AST.:=>: AST.ADExpr arg, Nothing AST.:=>: AST.ADExpr( AST.PrimLit (show len))]
425   }
426 genResize' (Right name) _ _ = error $ "\nGenerate.genFromSizedWord': Cannot generate builtin function call assigned to a VHDLName: " ++ show name
427
428 genTimes :: BuiltinBuilder
429 genTimes = genNoInsts $ genExprArgs $ genExprRes genTimes'
430 genTimes' :: Either CoreSyn.CoreBndr AST.VHDLName -> CoreSyn.CoreBndr -> [AST.Expr] -> TranslatorSession AST.Expr
431 genTimes' (Left res) f [arg1,arg2] = do {
432   ; let { ty = Var.varType res
433         ; (tycon, args) = Type.splitTyConApp ty
434         ; name = Name.getOccString (TyCon.tyConName tycon)
435         } ;
436   ; len <- case name of
437       "SizedInt" -> MonadState.lift tsType $ tfp_to_int (sized_int_len_ty ty)
438       "SizedWord" -> MonadState.lift tsType $ tfp_to_int (sized_word_len_ty ty)
439       "RangedWord" -> do {  ubound <- MonadState.lift tsType $ tfp_to_int (ranged_word_bound_ty ty)
440                          ;  let bitsize = floor (logBase 2 (fromInteger (toInteger ubound)))
441                          ;  return bitsize
442                          }
443   ; return $ AST.PrimFCall $ AST.FCall (AST.NSimple (mkVHDLBasicId resizeId))
444              [Nothing AST.:=>: AST.ADExpr (arg1 AST.:*: arg2), Nothing AST.:=>: AST.ADExpr( AST.PrimLit (show len))]
445   }
446 genTimes' (Right name) _ _ = error $ "\nGenerate.genTimes': Cannot generate builtin function call assigned to a VHDLName: " ++ show name
447
448 -- FIXME: I'm calling genLitArgs which is very specific function,
449 -- which needs to be fixed as well
450 genFromInteger :: BuiltinBuilder
451 genFromInteger = genNoInsts $ genLitArgs $ genExprRes genFromInteger'
452 genFromInteger' :: Either CoreSyn.CoreBndr AST.VHDLName -> CoreSyn.CoreBndr -> [Literal.Literal] -> TranslatorSession AST.Expr
453 genFromInteger' (Left res) f lits = do {
454   ; let { ty = Var.varType res
455         ; (tycon, args) = Type.splitTyConApp ty
456         ; name = Name.getOccString (TyCon.tyConName tycon)
457         } ;
458   ; len <- case name of
459     "SizedInt" -> MonadState.lift tsType $ tfp_to_int (sized_int_len_ty ty)
460     "SizedWord" -> MonadState.lift tsType $ tfp_to_int (sized_word_len_ty ty)
461     "RangedWord" -> do {
462       ; bound <- MonadState.lift tsType $ tfp_to_int (ranged_word_bound_ty ty)
463       ; return $ floor (logBase 2 (fromInteger (toInteger (bound)))) + 1
464       }
465   ; let fname = case name of "SizedInt" -> toSignedId ; "SizedWord" -> toUnsignedId ; "RangedWord" -> toUnsignedId
466   ; return $ AST.PrimFCall $ AST.FCall (AST.NSimple (mkVHDLBasicId fname))
467             [Nothing AST.:=>: AST.ADExpr (AST.PrimLit (show (last lits))), Nothing AST.:=>: AST.ADExpr( AST.PrimLit (show len))]
468
469   }
470
471 genFromInteger' (Right name) _ _ = error $ "\nGenerate.genFromInteger': Cannot generate builtin function call assigned to a VHDLName: " ++ show name
472
473 genSizedInt :: BuiltinBuilder
474 genSizedInt = genFromInteger
475
476 {-
477 -- | Generate a Builder for the builtin datacon TFVec
478 genTFVec :: BuiltinBuilder
479 genTFVec (Left res) f [Left (CoreSyn.Let (CoreSyn.Rec letBinders) letRes)] = do {
480   -- Generate Assignments for all the binders
481   ; letAssigns <- mapM genBinderAssign letBinders
482   -- Generate assignments for the result (which might be another let binding)
483   ; (resBinders,resAssignments) <- genResAssign letRes
484   -- Get all the Assigned binders
485   ; let assignedBinders = Maybe.catMaybes (map fst letAssigns)
486   -- Make signal names for all the assigned binders
487   ; sigs <- mapM (\x -> MonadState.lift tsType $ varToVHDLExpr x) (assignedBinders ++ resBinders)
488   -- Assign all the signals to the resulting vector
489   ; let { vecsigns = mkAggregateSignal sigs
490         ; vecassign = mkUncondAssign (Left res) vecsigns
491         } ;
492   -- Generate all the signal declaration for the assigned binders
493   ; sig_dec_maybes <- mapM mkSigDec (assignedBinders ++ resBinders)
494   ; let { sig_decs = map (AST.BDISD) (Maybe.catMaybes $ sig_dec_maybes)
495   -- Setup the VHDL Block
496         ; block_label = mkVHDLExtId ("TFVec_" ++ show (varToString res))
497         ; block = AST.BlockSm block_label [] (AST.PMapAspect []) sig_decs ((concat (map snd letAssigns)) ++ resAssignments ++ [vecassign])
498         } ;
499   -- Return the block statement coressponding to the TFVec literal
500   ; return $ [AST.CSBSm block]
501   }
502   where
503     genBinderAssign :: (CoreSyn.CoreBndr, CoreSyn.CoreExpr) -> TranslatorSession (Maybe CoreSyn.CoreBndr, [AST.ConcSm])
504     -- For now we only translate applications
505     genBinderAssign (bndr, app@(CoreSyn.App _ _)) = do
506       let (CoreSyn.Var f, args) = CoreSyn.collectArgs app
507       let valargs = get_val_args (Var.varType f) args
508       apps <- genApplication (Left bndr) f (map Left valargs)
509       return (Just bndr, apps)
510     genBinderAssign _ = return (Nothing,[])
511     genResAssign :: CoreSyn.CoreExpr -> TranslatorSession ([CoreSyn.CoreBndr], [AST.ConcSm])
512     genResAssign app@(CoreSyn.App _ letexpr) = do
513       case letexpr of
514         (CoreSyn.Let (CoreSyn.Rec letbndrs) letres) -> do
515           letapps <- mapM genBinderAssign letbndrs
516           let bndrs = Maybe.catMaybes (map fst letapps)
517           let app = (map snd letapps)
518           (vars, apps) <- genResAssign letres
519           return ((bndrs ++ vars),((concat app) ++ apps))
520         otherwise -> return ([],[])
521     genResAssign _ = return ([],[])
522
523 genTFVec (Left res) f [Left app@(CoreSyn.App _ _)] = do {
524   ; let { elems = reduceCoreListToHsList app
525   -- Make signal names for all the binders
526         ; binders = map (\expr -> case expr of 
527                           (CoreSyn.Var b) -> b
528                           otherwise -> error $ "\nGenerate.genTFVec: Cannot generate TFVec: " 
529                             ++ show res ++ ", with elems:\n" ++ show elems ++ "\n" ++ pprString elems) elems
530         } ;
531   ; sigs <- mapM (\x -> MonadState.lift tsType $ varToVHDLExpr x) binders
532   -- Assign all the signals to the resulting vector
533   ; let { vecsigns = mkAggregateSignal sigs
534         ; vecassign = mkUncondAssign (Left res) vecsigns
535   -- Setup the VHDL Block
536         ; block_label = mkVHDLExtId ("TFVec_" ++ show (varToString res))
537         ; block = AST.BlockSm block_label [] (AST.PMapAspect []) [] [vecassign]
538         } ;
539   -- Return the block statement coressponding to the TFVec literal
540   ; return $ [AST.CSBSm block]
541   }
542   
543 genTFVec (Left name) _ [Left xs] = error $ "\nGenerate.genTFVec: Cannot generate TFVec: " ++ show name ++ ", with elems:\n" ++ show xs ++ "\n" ++ pprString xs
544
545 genTFVec (Right name) _ _ = error $ "\nGenerate.genTFVec: Cannot generate TFVec assigned to VHDLName: " ++ show name
546 -}
547 -- | Generate a generate statement for the builtin function "map"
548 genMap :: BuiltinBuilder
549 genMap (Left res) f [Left mapped_f, Left (CoreSyn.Var arg)] = do {
550   -- mapped_f must be a CoreExpr (since we can't represent functions as VHDL
551   -- expressions). arg must be a CoreExpr (and should be a CoreSyn.Var), since
552   -- we must index it (which we couldn't if it was a VHDL Expr, since only
553   -- VHDLNames can be indexed).
554   -- Setup the generate scheme
555   ; len <- MonadState.lift tsType $ tfp_to_int $ (tfvec_len_ty . Var.varType) res
556           -- TODO: Use something better than varToString
557   ; let { label       = mkVHDLExtId ("mapVector" ++ (varToString res))
558         ; n_id        = mkVHDLBasicId "n"
559         ; n_expr      = idToVHDLExpr n_id
560         ; range       = AST.ToRange (AST.PrimLit "0") (AST.PrimLit $ show (len-1))
561         ; genScheme   = AST.ForGn n_id range
562           -- Create the content of the generate statement: Applying the mapped_f to
563           -- each of the elements in arg, storing to each element in res
564         ; resname     = mkIndexedName (varToVHDLName res) n_expr
565         ; argexpr     = vhdlNameToVHDLExpr $ mkIndexedName (varToVHDLName arg) n_expr
566         ; (CoreSyn.Var real_f, already_mapped_args) = CoreSyn.collectArgs mapped_f
567         ; valargs = get_val_args (Var.varType real_f) already_mapped_args
568         } ;
569   ; (app_concsms, used) <- genApplication (Right resname) real_f (map Left valargs ++ [Right argexpr])
570     -- Return the generate statement
571   ; return ([AST.CSGSm $ AST.GenerateSm label genScheme [] app_concsms], used)
572   }
573
574 genMap' (Right name) _ _ = error $ "\nGenerate.genMap': Cannot generate map function call assigned to a VHDLName: " ++ show name
575     
576 genZipWith :: BuiltinBuilder
577 genZipWith = genVarArgs genZipWith'
578 genZipWith' :: (Either CoreSyn.CoreBndr AST.VHDLName) -> CoreSyn.CoreBndr -> [Var.Var] -> TranslatorSession ([AST.ConcSm], [CoreSyn.CoreBndr])
579 genZipWith' (Left res) f args@[zipped_f, arg1, arg2] = do {
580   -- Setup the generate scheme
581   ; len <- MonadState.lift tsType $ tfp_to_int $ (tfvec_len_ty . Var.varType) res
582           -- TODO: Use something better than varToString
583   ; let { label       = mkVHDLExtId ("zipWithVector" ++ (varToString res))
584         ; n_id        = mkVHDLBasicId "n"
585         ; n_expr      = idToVHDLExpr n_id
586         ; range       = AST.ToRange (AST.PrimLit "0") (AST.PrimLit $ show (len-1))
587         ; genScheme   = AST.ForGn n_id range
588           -- Create the content of the generate statement: Applying the zipped_f to
589           -- each of the elements in arg1 and arg2, storing to each element in res
590         ; resname     = mkIndexedName (varToVHDLName res) n_expr
591         ; argexpr1    = vhdlNameToVHDLExpr $ mkIndexedName (varToVHDLName arg1) n_expr
592         ; argexpr2    = vhdlNameToVHDLExpr $ mkIndexedName (varToVHDLName arg2) n_expr
593         } ;
594   ; (app_concsms, used) <- genApplication (Right resname) zipped_f [Right argexpr1, Right argexpr2]
595     -- Return the generate functions
596   ; return ([AST.CSGSm $ AST.GenerateSm label genScheme [] app_concsms], used)
597   }
598
599 genFoldl :: BuiltinBuilder
600 genFoldl = genFold True
601
602 genFoldr :: BuiltinBuilder
603 genFoldr = genFold False
604
605 genFold :: Bool -> BuiltinBuilder
606 genFold left = genVarArgs (genFold' left)
607
608 genFold' :: Bool -> (Either CoreSyn.CoreBndr AST.VHDLName) -> CoreSyn.CoreBndr -> [Var.Var] -> TranslatorSession ([AST.ConcSm], [CoreSyn.CoreBndr])
609 genFold' left res f args@[folded_f , start ,vec]= do
610   len <- MonadState.lift tsType $ tfp_to_int (tfvec_len_ty (Var.varType vec))
611   genFold'' len left res f args
612
613 genFold'' :: Int -> Bool -> (Either CoreSyn.CoreBndr AST.VHDLName) -> CoreSyn.CoreBndr -> [Var.Var] -> TranslatorSession ([AST.ConcSm], [CoreSyn.CoreBndr])
614 -- Special case for an empty input vector, just assign start to res
615 genFold'' len left (Left res) _ [_, start, vec] | len == 0 = do
616   arg <- MonadState.lift tsType $ varToVHDLExpr start
617   return ([mkUncondAssign (Left res) arg], [])
618     
619 genFold'' len left (Left res) f [folded_f, start, vec] = do
620   -- The vector length
621   --len <- MonadState.lift tsType $ tfp_to_int $ (tfvec_len_ty . Var.varType) vec
622   -- An expression for len-1
623   let len_min_expr = (AST.PrimLit $ show (len-1))
624   -- evec is (TFVec n), so it still needs an element type
625   let (nvec, _) = Type.splitAppTy (Var.varType vec)
626   -- Put the type of the start value in nvec, this will be the type of our
627   -- temporary vector
628   let tmp_ty = Type.mkAppTy nvec (Var.varType start)
629   let error_msg = "\nGenerate.genFold': Can not construct temp vector for element type: " ++ pprString tmp_ty 
630   -- TODO: Handle Nothing
631   Just tmp_vhdl_ty <- MonadState.lift tsType $ vhdlTy error_msg tmp_ty
632   -- Setup the generate scheme
633   let gen_label = mkVHDLExtId ("foldlVector" ++ (varToString vec))
634   let block_label = mkVHDLExtId ("foldlVector" ++ (varToString res))
635   let gen_range = if left then AST.ToRange (AST.PrimLit "0") len_min_expr
636                   else AST.DownRange len_min_expr (AST.PrimLit "0")
637   let gen_scheme   = AST.ForGn n_id gen_range
638   -- Make the intermediate vector
639   let  tmp_dec     = AST.BDISD $ AST.SigDec tmp_id tmp_vhdl_ty Nothing
640   -- Create the generate statement
641   cells' <- sequence [genFirstCell, genOtherCell]
642   let (cells, useds) = unzip cells'
643   let gen_sm = AST.GenerateSm gen_label gen_scheme [] (map AST.CSGSm cells)
644   -- Assign tmp[len-1] or tmp[0] to res
645   let out_assign = mkUncondAssign (Left res) $ vhdlNameToVHDLExpr (if left then
646                     (mkIndexedName tmp_name (AST.PrimLit $ show (len-1))) else
647                     (mkIndexedName tmp_name (AST.PrimLit "0")))      
648   let block = AST.BlockSm block_label [] (AST.PMapAspect []) [tmp_dec] [AST.CSGSm gen_sm, out_assign]
649   return ([AST.CSBSm block], concat useds)
650   where
651     -- An id for the counter
652     n_id = mkVHDLBasicId "n"
653     n_cur = idToVHDLExpr n_id
654     -- An expression for previous n
655     n_prev = if left then (n_cur AST.:-: (AST.PrimLit "1"))
656                      else (n_cur AST.:+: (AST.PrimLit "1"))
657     -- An id for the tmp result vector
658     tmp_id = mkVHDLBasicId "tmp"
659     tmp_name = AST.NSimple tmp_id
660     -- Generate parts of the fold
661     genFirstCell, genOtherCell :: TranslatorSession (AST.GenerateSm, [CoreSyn.CoreBndr])
662     genFirstCell = do
663       len <- MonadState.lift tsType $ tfp_to_int $ (tfvec_len_ty . Var.varType) vec
664       let cond_label = mkVHDLExtId "firstcell"
665       -- if n == 0 or n == len-1
666       let cond_scheme = AST.IfGn $ n_cur AST.:=: (if left then (AST.PrimLit "0")
667                                                   else (AST.PrimLit $ show (len-1)))
668       -- Output to tmp[current n]
669       let resname = mkIndexedName tmp_name n_cur
670       -- Input from start
671       argexpr1 <- MonadState.lift tsType $ varToVHDLExpr start
672       -- Input from vec[current n]
673       let argexpr2 = vhdlNameToVHDLExpr $ mkIndexedName (varToVHDLName vec) n_cur
674       (app_concsms, used) <- genApplication (Right resname) folded_f  ( if left then
675                                                                   [Right argexpr1, Right argexpr2]
676                                                                 else
677                                                                   [Right argexpr2, Right argexpr1]
678                                                               )
679       -- Return the conditional generate part
680       return (AST.GenerateSm cond_label cond_scheme [] app_concsms, used)
681
682     genOtherCell = do
683       len <- MonadState.lift tsType $ tfp_to_int $ (tfvec_len_ty . Var.varType) vec
684       let cond_label = mkVHDLExtId "othercell"
685       -- if n > 0 or n < len-1
686       let cond_scheme = AST.IfGn $ n_cur AST.:/=: (if left then (AST.PrimLit "0")
687                                                    else (AST.PrimLit $ show (len-1)))
688       -- Output to tmp[current n]
689       let resname = mkIndexedName tmp_name n_cur
690       -- Input from tmp[previous n]
691       let argexpr1 = vhdlNameToVHDLExpr $ mkIndexedName tmp_name n_prev
692       -- Input from vec[current n]
693       let argexpr2 = vhdlNameToVHDLExpr $ mkIndexedName (varToVHDLName vec) n_cur
694       (app_concsms, used) <- genApplication (Right resname) folded_f  ( if left then
695                                                                   [Right argexpr1, Right argexpr2]
696                                                                 else
697                                                                   [Right argexpr2, Right argexpr1]
698                                                               )
699       -- Return the conditional generate part
700       return (AST.GenerateSm cond_label cond_scheme [] app_concsms, used)
701
702 -- | Generate a generate statement for the builtin function "zip"
703 genZip :: BuiltinBuilder
704 genZip = genNoInsts $ genVarArgs genZip'
705 genZip' :: (Either CoreSyn.CoreBndr AST.VHDLName) -> CoreSyn.CoreBndr -> [Var.Var] -> TranslatorSession [AST.ConcSm]
706 genZip' (Left res) f args@[arg1, arg2] = do {
707     -- Setup the generate scheme
708   ; len <- MonadState.lift tsType $ tfp_to_int $ (tfvec_len_ty . Var.varType) res
709           -- TODO: Use something better than varToString
710   ; let { label           = mkVHDLExtId ("zipVector" ++ (varToString res))
711         ; n_id            = mkVHDLBasicId "n"
712         ; n_expr          = idToVHDLExpr n_id
713         ; range           = AST.ToRange (AST.PrimLit "0") (AST.PrimLit $ show (len-1))
714         ; genScheme       = AST.ForGn n_id range
715         ; resname'        = mkIndexedName (varToVHDLName res) n_expr
716         ; argexpr1        = vhdlNameToVHDLExpr $ mkIndexedName (varToVHDLName arg1) n_expr
717         ; argexpr2        = vhdlNameToVHDLExpr $ mkIndexedName (varToVHDLName arg2) n_expr
718         } ; 
719   ; labels <- MonadState.lift tsType $ getFieldLabels (tfvec_elem (Var.varType res))
720   ; let { resnameA    = mkSelectedName resname' (labels!!0)
721         ; resnameB    = mkSelectedName resname' (labels!!1)
722         ; resA_assign = mkUncondAssign (Right resnameA) argexpr1
723         ; resB_assign = mkUncondAssign (Right resnameB) argexpr2
724         } ;
725     -- Return the generate functions
726   ; return [AST.CSGSm $ AST.GenerateSm label genScheme [] [resA_assign,resB_assign]]
727   }
728   
729 -- | Generate a generate statement for the builtin function "fst"
730 genFst :: BuiltinBuilder
731 genFst = genNoInsts $ genVarArgs genFst'
732 genFst' :: (Either CoreSyn.CoreBndr AST.VHDLName) -> CoreSyn.CoreBndr -> [Var.Var] -> TranslatorSession [AST.ConcSm]
733 genFst' (Left res) f args@[arg] = do {
734   ; labels <- MonadState.lift tsType $ getFieldLabels (Var.varType arg)
735   ; let { argexpr'    = varToVHDLName arg
736         ; argexprA    = vhdlNameToVHDLExpr $ mkSelectedName argexpr' (labels!!0)
737         ; assign      = mkUncondAssign (Left res) argexprA
738         } ;
739     -- Return the generate functions
740   ; return [assign]
741   }
742   
743 -- | Generate a generate statement for the builtin function "snd"
744 genSnd :: BuiltinBuilder
745 genSnd = genNoInsts $ genVarArgs genSnd'
746 genSnd' :: (Either CoreSyn.CoreBndr AST.VHDLName) -> CoreSyn.CoreBndr -> [Var.Var] -> TranslatorSession [AST.ConcSm]
747 genSnd' (Left res) f args@[arg] = do {
748   ; labels <- MonadState.lift tsType $ getFieldLabels (Var.varType arg)
749   ; let { argexpr'    = varToVHDLName arg
750         ; argexprB    = vhdlNameToVHDLExpr $ mkSelectedName argexpr' (labels!!1)
751         ; assign      = mkUncondAssign (Left res) argexprB
752         } ;
753     -- Return the generate functions
754   ; return [assign]
755   }
756     
757 -- | Generate a generate statement for the builtin function "unzip"
758 genUnzip :: BuiltinBuilder
759 genUnzip = genNoInsts $ genVarArgs genUnzip'
760 genUnzip' :: (Either CoreSyn.CoreBndr AST.VHDLName) -> CoreSyn.CoreBndr -> [Var.Var] -> TranslatorSession [AST.ConcSm]
761 genUnzip' (Left res) f args@[arg] = do
762   let error_msg = "\nGenerate.genUnzip: Cannot generate unzip call: " ++ pprString res ++ " = " ++ pprString f ++ " " ++ pprString arg
763   htype <- MonadState.lift tsType $ mkHType error_msg (Var.varType arg)
764   -- Prepare a unconditional assignment, for the case when either part
765   -- of the unzip is a state variable, which will disappear in the
766   -- resulting VHDL, making the the unzip no longer required.
767   case htype of
768     -- A normal vector containing two-tuples
769     VecType _ (AggrType _ [_, _]) -> do {
770         -- Setup the generate scheme
771       ; len <- MonadState.lift tsType $ tfp_to_int $ (tfvec_len_ty . Var.varType) arg
772         -- TODO: Use something better than varToString
773       ; let { label           = mkVHDLExtId ("unzipVector" ++ (varToString res))
774             ; n_id            = mkVHDLBasicId "n"
775             ; n_expr          = idToVHDLExpr n_id
776             ; range           = AST.ToRange (AST.PrimLit "0") (AST.PrimLit $ show (len-1))
777             ; genScheme       = AST.ForGn n_id range
778             ; resname'        = varToVHDLName res
779             ; argexpr'        = mkIndexedName (varToVHDLName arg) n_expr
780             } ;
781       ; reslabels <- MonadState.lift tsType $ getFieldLabels (Var.varType res)
782       ; arglabels <- MonadState.lift tsType $ getFieldLabels (tfvec_elem (Var.varType arg))
783       ; let { resnameA    = mkIndexedName (mkSelectedName resname' (reslabels!!0)) n_expr
784             ; resnameB    = mkIndexedName (mkSelectedName resname' (reslabels!!1)) n_expr
785             ; argexprA    = vhdlNameToVHDLExpr $ mkSelectedName argexpr' (arglabels!!0)
786             ; argexprB    = vhdlNameToVHDLExpr $ mkSelectedName argexpr' (arglabels!!1)
787             ; resA_assign = mkUncondAssign (Right resnameA) argexprA
788             ; resB_assign = mkUncondAssign (Right resnameB) argexprB
789             } ;
790         -- Return the generate functions
791       ; return [AST.CSGSm $ AST.GenerateSm label genScheme [] [resA_assign,resB_assign]]
792       }
793     -- Both elements of the tuple were state, so they've disappeared. No
794     -- need to do anything
795     VecType _ (AggrType _ []) -> return []
796     -- A vector containing aggregates with more than two elements?
797     VecType _ (AggrType _ _) -> error $ "Unzipping a value that is not a vector of two-tuples? Value: " ++ pprString arg ++ "\nType: " ++ pprString (Var.varType arg)
798     -- One of the elements of the tuple was state, so there won't be a
799     -- tuple (record) in the VHDL output. We can just do a plain
800     -- assignment, then.
801     VecType _ _ -> do
802       argexpr <- MonadState.lift tsType $ varToVHDLExpr arg
803       return [mkUncondAssign (Left res) argexpr]
804     _ -> error $ "Unzipping a value that is not a vector? Value: " ++ pprString arg ++ "\nType: " ++ pprString (Var.varType arg) ++ "\nhtype: " ++ show htype
805
806 genCopy :: BuiltinBuilder 
807 genCopy = genNoInsts $ genVarArgs genCopy'
808 genCopy' :: (Either CoreSyn.CoreBndr AST.VHDLName ) -> CoreSyn.CoreBndr -> [Var.Var] -> TranslatorSession [AST.ConcSm]
809 genCopy' (Left res) f args@[arg] =
810   let
811     resExpr = AST.Aggregate [AST.ElemAssoc (Just AST.Others) 
812                 (AST.PrimName (varToVHDLName arg))]
813     out_assign = mkUncondAssign (Left res) resExpr
814   in 
815     return [out_assign]
816     
817 genConcat :: BuiltinBuilder
818 genConcat = genNoInsts $ genVarArgs genConcat'
819 genConcat' :: (Either CoreSyn.CoreBndr AST.VHDLName) -> CoreSyn.CoreBndr -> [Var.Var] -> TranslatorSession [AST.ConcSm]
820 genConcat' (Left res) f args@[arg] = do {
821     -- Setup the generate scheme
822   ; len1 <- MonadState.lift tsType $ tfp_to_int $ (tfvec_len_ty . Var.varType) arg
823   ; let (_, nvec) = Type.splitAppTy (Var.varType arg)
824   ; len2 <- MonadState.lift tsType $ tfp_to_int $ tfvec_len_ty nvec
825           -- TODO: Use something better than varToString
826   ; let { label       = mkVHDLExtId ("concatVector" ++ (varToString res))
827         ; n_id        = mkVHDLBasicId "n"
828         ; n_expr      = idToVHDLExpr n_id
829         ; fromRange   = n_expr AST.:*: (AST.PrimLit $ show len2)
830         ; genScheme   = AST.ForGn n_id range
831           -- Create the content of the generate statement: Applying the mapped_f to
832           -- each of the elements in arg, storing to each element in res
833         ; toRange     = (n_expr AST.:*: (AST.PrimLit $ show len2)) AST.:+: (AST.PrimLit $ show (len2-1))
834         ; range       = AST.ToRange (AST.PrimLit "0") (AST.PrimLit $ show (len1-1))
835         ; resname     = vecSlice fromRange toRange
836         ; argexpr     = vhdlNameToVHDLExpr $ mkIndexedName (varToVHDLName arg) n_expr
837         ; out_assign  = mkUncondAssign (Right resname) argexpr
838         } ;
839     -- Return the generate statement
840   ; return [AST.CSGSm $ AST.GenerateSm label genScheme [] [out_assign]]
841   }
842   where
843     vecSlice init last =  AST.NSlice (AST.SliceName (varToVHDLName res) 
844                             (AST.ToRange init last))
845
846 genIteraten :: BuiltinBuilder
847 genIteraten dst f args = genIterate dst f (tail args)
848
849 genIterate :: BuiltinBuilder
850 genIterate = genIterateOrGenerate True
851
852 genGeneraten :: BuiltinBuilder
853 genGeneraten dst f args = genGenerate dst f (tail args)
854
855 genGenerate :: BuiltinBuilder
856 genGenerate = genIterateOrGenerate False
857
858 genIterateOrGenerate :: Bool -> BuiltinBuilder
859 genIterateOrGenerate iter = genVarArgs (genIterateOrGenerate' iter)
860
861 genIterateOrGenerate' :: Bool -> (Either CoreSyn.CoreBndr AST.VHDLName) -> CoreSyn.CoreBndr -> [Var.Var] -> TranslatorSession ([AST.ConcSm], [CoreSyn.CoreBndr])
862 genIterateOrGenerate' iter (Left res) f args = do
863   len <- MonadState.lift tsType $ tfp_to_int ((tfvec_len_ty . Var.varType) res)
864   genIterateOrGenerate'' len iter (Left res) f args
865
866 genIterateOrGenerate'' :: Int -> Bool -> (Either CoreSyn.CoreBndr AST.VHDLName) -> CoreSyn.CoreBndr -> [Var.Var] -> TranslatorSession ([AST.ConcSm], [CoreSyn.CoreBndr])
867 -- Special case for an empty input vector, just assign start to res
868 genIterateOrGenerate'' len iter (Left res) _ [app_f, start] | len == 0 = return ([mkUncondAssign (Left res) (AST.PrimLit "\"\"")], [])
869
870 genIterateOrGenerate'' len iter (Left res) f [app_f, start] = do
871   -- The vector length
872   -- len <- MonadState.lift tsType $ tfp_to_int ((tfvec_len_ty . Var.varType) res)
873   -- An expression for len-1
874   let len_min_expr = (AST.PrimLit $ show (len-1))
875   -- -- evec is (TFVec n), so it still needs an element type
876   -- let (nvec, _) = splitAppTy (Var.varType vec)
877   -- -- Put the type of the start value in nvec, this will be the type of our
878   -- -- temporary vector
879   let tmp_ty = Var.varType res
880   let error_msg = "\nGenerate.genFold': Can not construct temp vector for element type: " ++ pprString tmp_ty 
881   -- TODO: Handle Nothing
882   Just tmp_vhdl_ty <- MonadState.lift tsType $ vhdlTy error_msg tmp_ty
883   -- Setup the generate scheme
884   let gen_label = mkVHDLExtId ("iterateVector" ++ (varToString start))
885   let block_label = mkVHDLExtId ("iterateVector" ++ (varToString res))
886   let gen_range = AST.ToRange (AST.PrimLit "0") len_min_expr
887   let gen_scheme   = AST.ForGn n_id gen_range
888   -- Make the intermediate vector
889   let  tmp_dec     = AST.BDISD $ AST.SigDec tmp_id tmp_vhdl_ty Nothing
890   -- Create the generate statement
891   cells' <- sequence [genFirstCell, genOtherCell]
892   let (cells, useds) = unzip cells'
893   let gen_sm = AST.GenerateSm gen_label gen_scheme [] (map AST.CSGSm cells)
894   -- Assign tmp[len-1] or tmp[0] to res
895   let out_assign = mkUncondAssign (Left res) $ vhdlNameToVHDLExpr tmp_name    
896   let block = AST.BlockSm block_label [] (AST.PMapAspect []) [tmp_dec] [AST.CSGSm gen_sm, out_assign]
897   return ([AST.CSBSm block], concat useds)
898   where
899     -- An id for the counter
900     n_id = mkVHDLBasicId "n"
901     n_cur = idToVHDLExpr n_id
902     -- An expression for previous n
903     n_prev = n_cur AST.:-: (AST.PrimLit "1")
904     -- An id for the tmp result vector
905     tmp_id = mkVHDLBasicId "tmp"
906     tmp_name = AST.NSimple tmp_id
907     -- Generate parts of the fold
908     genFirstCell, genOtherCell :: TranslatorSession (AST.GenerateSm, [CoreSyn.CoreBndr])
909     genFirstCell = do
910       let cond_label = mkVHDLExtId "firstcell"
911       -- if n == 0 or n == len-1
912       let cond_scheme = AST.IfGn $ n_cur AST.:=: (AST.PrimLit "0")
913       -- Output to tmp[current n]
914       let resname = mkIndexedName tmp_name n_cur
915       -- Input from start
916       argexpr <- MonadState.lift tsType $ varToVHDLExpr start
917       let startassign = mkUncondAssign (Right resname) argexpr
918       (app_concsms, used) <- genApplication (Right resname) app_f  [Right argexpr]
919       -- Return the conditional generate part
920       let gensm = AST.GenerateSm cond_label cond_scheme [] (if iter then 
921                                                           [startassign]
922                                                          else 
923                                                           app_concsms
924                                                         )
925       return (gensm, used)
926
927     genOtherCell = do
928       let cond_label = mkVHDLExtId "othercell"
929       -- if n > 0 or n < len-1
930       let cond_scheme = AST.IfGn $ n_cur AST.:/=: (AST.PrimLit "0")
931       -- Output to tmp[current n]
932       let resname = mkIndexedName tmp_name n_cur
933       -- Input from tmp[previous n]
934       let argexpr = vhdlNameToVHDLExpr $ mkIndexedName tmp_name n_prev
935       (app_concsms, used) <- genApplication (Right resname) app_f [Right argexpr]
936       -- Return the conditional generate part
937       return (AST.GenerateSm cond_label cond_scheme [] app_concsms, used)
938
939 genBlockRAM :: BuiltinBuilder
940 genBlockRAM = genNoInsts $ genExprArgs genBlockRAM'
941
942 genBlockRAM' :: (Either CoreSyn.CoreBndr AST.VHDLName) -> CoreSyn.CoreBndr -> [AST.Expr] -> TranslatorSession [AST.ConcSm]
943 genBlockRAM' (Left res) f args@[data_in,rdaddr,wraddr,wrenable] = do
944   -- Get the ram type
945   let (tup,data_out) = Type.splitAppTy (Var.varType res)
946   let (tup',ramvec) = Type.splitAppTy tup
947   let Just realram = Type.coreView ramvec
948   let Just (tycon, types) = Type.splitTyConApp_maybe realram
949   Just ram_vhdl_ty <- MonadState.lift tsType $ vhdlTy "wtf" (head types)
950   -- Make the intermediate vector
951   let ram_dec = AST.BDISD $ AST.SigDec ram_id ram_vhdl_ty Nothing
952   -- Get the data_out name
953   -- reslabels <- MonadState.lift tsType $ getFieldLabels (Var.varType res)
954   let resname = varToVHDLName res
955   -- let resname = mkSelectedName resname' (reslabels!!0)
956   let rdaddr_int = genExprFCall (mkVHDLBasicId toIntegerId) rdaddr
957   let argexpr = vhdlNameToVHDLExpr $ mkIndexedName (AST.NSimple ram_id) rdaddr_int
958   let assign = mkUncondAssign (Right resname) argexpr
959   let block_label = mkVHDLExtId ("blockRAM" ++ (varToString res))
960   let block = AST.BlockSm block_label [] (AST.PMapAspect []) [ram_dec] [assign, mkUpdateProcSm]
961   return [AST.CSBSm block]
962   where
963     ram_id = mkVHDLBasicId "ram"
964     mkUpdateProcSm :: AST.ConcSm
965     mkUpdateProcSm = AST.CSPSm $ AST.ProcSm proclabel [clockId] [statement]
966       where
967         proclabel   = mkVHDLBasicId "updateRAM"
968         rising_edge = mkVHDLBasicId "rising_edge"
969         wraddr_int  = genExprFCall (mkVHDLBasicId toIntegerId) wraddr
970         ramloc      = mkIndexedName (AST.NSimple ram_id) wraddr_int
971         wform       = AST.Wform [AST.WformElem data_in Nothing]
972         ramassign      = AST.SigAssign ramloc wform
973         rising_edge_clk = genExprFCall rising_edge (AST.PrimName $ AST.NSimple clockId)
974         statement   = AST.IfSm (AST.And rising_edge_clk wrenable) [ramassign] [] Nothing
975         
976 genSplit :: BuiltinBuilder
977 genSplit = genNoInsts $ genVarArgs genSplit'
978
979 genSplit' :: (Either CoreSyn.CoreBndr AST.VHDLName) -> CoreSyn.CoreBndr -> [Var.Var] -> TranslatorSession [AST.ConcSm]
980 genSplit' (Left res) f args@[vecIn] = do {
981   ; labels <- MonadState.lift tsType $ getFieldLabels (Var.varType res)
982   ; len <- MonadState.lift tsType $ tfp_to_int $ (tfvec_len_ty . Var.varType) vecIn
983   ; let { block_label = mkVHDLExtId ("split" ++ (varToString vecIn))
984         ; halflen   = round ((fromIntegral len) / 2)
985         ; rangeL    = vecSlice (AST.PrimLit "0") (AST.PrimLit $ show (halflen - 1))
986         ; rangeR    = vecSlice (AST.PrimLit $ show halflen) (AST.PrimLit $ show (len - 1))
987         ; resname   = varToVHDLName res
988         ; resnameL  = mkSelectedName resname (labels!!0)
989         ; resnameR  = mkSelectedName resname (labels!!1)
990         ; argexprL  = vhdlNameToVHDLExpr rangeL
991         ; argexprR  = vhdlNameToVHDLExpr rangeR
992         ; out_assignL = mkUncondAssign (Right resnameL) argexprL
993         ; out_assignR = mkUncondAssign (Right resnameR) argexprR
994         ; block = AST.BlockSm block_label [] (AST.PMapAspect []) [] [out_assignL, out_assignR]
995         }
996   ; return [AST.CSBSm block]
997   }
998   where
999     vecSlice init last =  AST.NSlice (AST.SliceName (varToVHDLName res) 
1000                             (AST.ToRange init last))
1001 -----------------------------------------------------------------------------
1002 -- Function to generate VHDL for applications
1003 -----------------------------------------------------------------------------
1004 genApplication ::
1005   (Either CoreSyn.CoreBndr AST.VHDLName) -- ^ Where to store the result?
1006   -> CoreSyn.CoreBndr -- ^ The function to apply
1007   -> [Either CoreSyn.CoreExpr AST.Expr] -- ^ The arguments to apply
1008   -> TranslatorSession ([AST.ConcSm], [CoreSyn.CoreBndr]) 
1009   -- ^ The corresponding VHDL concurrent statements and entities
1010   --   instantiated.
1011 genApplication dst f args = do
1012   nonemptydst <- case dst of
1013     Left bndr -> hasNonEmptyType bndr 
1014     Right _ -> return True
1015   if nonemptydst
1016     then
1017       if Var.isGlobalId f then
1018         case Var.idDetails f of
1019           IdInfo.DataConWorkId dc -> case dst of
1020             -- It's a datacon. Create a record from its arguments.
1021             Left bndr -> do
1022               -- We have the bndr, so we can get at the type
1023               htype <- MonadState.lift tsType $ mkHTypeEither (Var.varType bndr)
1024               let argsNostate = filter (\x -> not (either hasStateType (\x -> False) x)) args
1025               case argsNostate of
1026                 [arg] -> do
1027                   [arg'] <- argsToVHDLExprs [arg]
1028                   return ([mkUncondAssign dst arg'], [])
1029                 otherwise ->
1030                   case htype of
1031                     Right (AggrType _ _) -> do
1032                       labels <- MonadState.lift tsType $ getFieldLabels (Var.varType bndr)
1033                       args' <- argsToVHDLExprs argsNostate
1034                       return (zipWith mkassign labels args', [])
1035                       where
1036                         mkassign :: AST.VHDLId -> AST.Expr -> AST.ConcSm
1037                         mkassign label arg =
1038                           let sel_name = mkSelectedName ((either varToVHDLName id) dst) label in
1039                           mkUncondAssign (Right sel_name) arg
1040                     _ -> do -- error $ "DIE!"
1041                       args' <- argsToVHDLExprs argsNostate
1042                       return ([mkUncondAssign dst (head args')], [])            
1043             Right _ -> error "\nGenerate.genApplication(DataConWorkId): Can't generate dataconstructor application without an original binder"
1044           IdInfo.DataConWrapId dc -> case dst of
1045             -- It's a datacon. Create a record from its arguments.
1046             Left bndr ->
1047               case (Map.lookup (varToString f) globalNameTable) of
1048                Just (arg_count, builder) ->
1049                 if length args == arg_count then
1050                   builder dst f args
1051                 else
1052                   error $ "\nGenerate.genApplication(DataConWrapId): Incorrect number of arguments to builtin function: " ++ pprString f ++ " Args: " ++ show args
1053                Nothing -> error $ "\nGenerate.genApplication(DataConWrapId): Can't generate dataconwrapper: " ++ (show dc)
1054             Right _ -> error "\nGenerate.genApplication(DataConWrapId): Can't generate dataconwrapper application without an original binder"
1055           IdInfo.VanillaId ->
1056             -- It's a global value imported from elsewhere. These can be builtin
1057             -- functions. Look up the function name in the name table and execute
1058             -- the associated builder if there is any and the argument count matches
1059             -- (this should always be the case if it typechecks, but just to be
1060             -- sure...).
1061             case (Map.lookup (varToString f) globalNameTable) of
1062               Just (arg_count, builder) ->
1063                 if length args == arg_count then
1064                   builder dst f args
1065                 else
1066                   error $ "\nGenerate.genApplication(VanillaId): Incorrect number of arguments to builtin function: " ++ pprString f ++ " Args: " ++ show args
1067               Nothing -> do
1068                 top <- isTopLevelBinder f
1069                 if top then
1070                   do
1071                     -- Local binder that references a top level binding.  Generate a
1072                     -- component instantiation.
1073                     signature <- getEntity f
1074                     args' <- argsToVHDLExprs args
1075                     let entity_id = ent_id signature
1076                     -- TODO: Using show here isn't really pretty, but we'll need some
1077                     -- unique-ish value...
1078                     let label = "comp_ins_" ++ (either show prettyShow) dst
1079                     let portmaps = mkAssocElems args' ((either varToVHDLName id) dst) signature
1080                     return ([mkComponentInst label entity_id portmaps], [f])
1081                   else
1082                     -- Not a top level binder, so this must be a local variable reference.
1083                     -- It should have a representable type (and thus, no arguments) and a
1084                     -- signal should be generated for it. Just generate an unconditional
1085                     -- assignment here.
1086                     -- FIXME : I DONT KNOW IF THE ABOVE COMMENT HOLDS HERE, SO FOR NOW JUST ERROR!
1087                     -- f' <- MonadState.lift tsType $ varToVHDLExpr f
1088                     --                   return $ ([mkUncondAssign dst f'], [])
1089                   do errtype <- case dst of 
1090                         Left bndr -> do 
1091                           htype <- MonadState.lift tsType $ mkHTypeEither (Var.varType bndr)
1092                           return (show htype)
1093                         Right vhd -> return $ show vhd
1094                      error ("\nGenerate.genApplication(VanillaId): Using function from another module that is not a known builtin: " ++ (pprString f) ++ "::" ++ errtype) 
1095           IdInfo.ClassOpId cls ->
1096             -- FIXME: Not looking for what instance this class op is called for
1097             -- Is quite stupid of course.
1098             case (Map.lookup (varToString f) globalNameTable) of
1099               Just (arg_count, builder) ->
1100                 if length args == arg_count then
1101                   builder dst f args
1102                 else
1103                   error $ "\nGenerate.genApplication(ClassOpId): Incorrect number of arguments to builtin function: " ++ pprString f ++ " Args: " ++ show args
1104               Nothing -> error $ "\nGenerate.genApplication(ClassOpId): Using function from another module that is not a known builtin: " ++ pprString f
1105           details -> error $ "\nGenerate.genApplication: Calling unsupported function " ++ pprString f ++ " with GlobalIdDetails " ++ pprString details
1106         else do
1107           top <- isTopLevelBinder f
1108           if top then
1109             do
1110                -- Local binder that references a top level binding.  Generate a
1111                -- component instantiation.
1112                signature <- getEntity f
1113                args' <- argsToVHDLExprs args
1114                let entity_id = ent_id signature
1115                -- TODO: Using show here isn't really pretty, but we'll need some
1116                -- unique-ish value...
1117                let label = "comp_ins_" ++ (either (prettyShow . varToVHDLName) prettyShow) dst
1118                let portmaps = mkAssocElems args' ((either varToVHDLName id) dst) signature
1119                return ([mkComponentInst label entity_id portmaps], [f])
1120             else
1121               -- Not a top level binder, so this must be a local variable reference.
1122               -- It should have a representable type (and thus, no arguments) and a
1123               -- signal should be generated for it. Just generate an unconditional
1124               -- assignment here.
1125             do f' <- MonadState.lift tsType $ varToVHDLExpr f
1126                return ([mkUncondAssign dst f'], [])
1127     else -- Destination has empty type, don't generate anything
1128       return ([], [])
1129 -----------------------------------------------------------------------------
1130 -- Functions to generate functions dealing with vectors.
1131 -----------------------------------------------------------------------------
1132
1133 -- Returns the VHDLId of the vector function with the given name for the given
1134 -- element type. Generates -- this function if needed.
1135 vectorFunId :: Type.Type -> String -> TypeSession AST.VHDLId
1136 vectorFunId el_ty fname = do
1137   let error_msg = "\nGenerate.vectorFunId: Can not construct vector function for element: " ++ pprString el_ty
1138   -- TODO: Handle the Nothing case?
1139   elemTM_maybe <- vhdlTy error_msg el_ty
1140   let elemTM = Maybe.fromMaybe
1141                  (error $ "\nGenerate.vectorFunId: Cannot generate vector function \"" ++ fname ++ "\" for the empty type \"" ++ (pprString el_ty) ++ "\"")
1142                  elemTM_maybe
1143   -- TODO: This should not be duplicated from mk_vector_ty. Probably but it in
1144   -- the VHDLState or something.
1145   let vectorTM = mkVHDLExtId $ "vector_" ++ (AST.fromVHDLId elemTM)
1146   typefuns <- MonadState.get tsTypeFuns
1147   el_htype <- mkHType error_msg el_ty
1148   case Map.lookup (UVecType el_htype, fname) typefuns of
1149     -- Function already generated, just return it
1150     Just (id, _) -> return id
1151     -- Function not generated yet, generate it
1152     Nothing -> do
1153       let functions = genUnconsVectorFuns elemTM vectorTM
1154       case lookup fname functions of
1155         Just body -> do
1156           MonadState.modify tsTypeFuns $ Map.insert (UVecType el_htype, fname) (function_id, (fst body))
1157           mapM_ (vectorFunId el_ty) (snd body)
1158           return function_id
1159         Nothing -> error $ "\nGenerate.vectorFunId: I don't know how to generate vector function " ++ fname
1160   where
1161     function_id = mkVHDLExtId fname
1162
1163 genUnconsVectorFuns :: AST.TypeMark -- ^ type of the vector elements
1164                     -> AST.TypeMark -- ^ type of the vector
1165                     -> [(String, (AST.SubProgBody, [String]))]
1166 genUnconsVectorFuns elemTM vectorTM  = 
1167   [ (exId, (AST.SubProgBody exSpec      []                  [exExpr],[]))
1168   , (replaceId, (AST.SubProgBody replaceSpec [AST.SPVD replaceVar] [replaceExpr1,replaceExpr2,replaceRet],[]))
1169   , (lastId, (AST.SubProgBody lastSpec    []                  [lastExpr],[]))
1170   , (initId, (AST.SubProgBody initSpec    [AST.SPVD initVar]  [initExpr, initRet],[]))
1171   , (minimumId, (AST.SubProgBody minimumSpec [] [minimumExpr],[]))
1172   , (takeId, (AST.SubProgBody takeSpec    [AST.SPVD takeVar]  [takeExpr, takeRet],[minimumId]))
1173   , (dropId, (AST.SubProgBody dropSpec    [AST.SPVD dropVar]  [dropExpr, dropRet],[]))
1174   , (plusgtId, (AST.SubProgBody plusgtSpec  [AST.SPVD plusgtVar] [plusgtExpr, plusgtRet],[]))
1175   , (emptyId, (AST.SubProgBody emptySpec   [AST.SPVD emptyVar] [emptyExpr],[]))
1176   , (singletonId, (AST.SubProgBody singletonSpec [AST.SPVD singletonVar] [singletonRet],[]))
1177   , (copynId, (AST.SubProgBody copynSpec    [AST.SPVD copynVar]      [copynExpr],[]))
1178   , (selId, (AST.SubProgBody selSpec  [AST.SPVD selVar] [selFor, selRet],[]))
1179   , (ltplusId, (AST.SubProgBody ltplusSpec [AST.SPVD ltplusVar] [ltplusExpr, ltplusRet],[]))  
1180   , (plusplusId, (AST.SubProgBody plusplusSpec [AST.SPVD plusplusVar] [plusplusExpr, plusplusRet],[]))
1181   , (lengthTId, (AST.SubProgBody lengthTSpec [] [lengthTExpr],[]))
1182   , (shiftlId, (AST.SubProgBody shiftlSpec [AST.SPVD shiftlVar] [shiftlExpr, shiftlRet], [initId]))
1183   , (shiftrId, (AST.SubProgBody shiftrSpec [AST.SPVD shiftrVar] [shiftrExpr, shiftrRet], [tailId]))
1184   , (nullId, (AST.SubProgBody nullSpec [] [nullExpr], []))
1185   , (rotlId, (AST.SubProgBody rotlSpec [AST.SPVD rotlVar] [rotlExpr, rotlRet], [nullId, lastId, initId]))
1186   , (rotrId, (AST.SubProgBody rotrSpec [AST.SPVD rotrVar] [rotrExpr, rotrRet], [nullId, tailId, headId]))
1187   , (reverseId, (AST.SubProgBody reverseSpec [AST.SPVD reverseVar] [reverseFor, reverseRet], []))
1188   ]
1189   where 
1190     ixPar   = AST.unsafeVHDLBasicId "ix"
1191     vecPar  = AST.unsafeVHDLBasicId "vec"
1192     vec1Par = AST.unsafeVHDLBasicId "vec1"
1193     vec2Par = AST.unsafeVHDLBasicId "vec2"
1194     nPar    = AST.unsafeVHDLBasicId "n"
1195     leftPar = AST.unsafeVHDLBasicId "nLeft"
1196     rightPar = AST.unsafeVHDLBasicId "nRight"
1197     iId     = AST.unsafeVHDLBasicId "i"
1198     iPar    = iId
1199     aPar    = AST.unsafeVHDLBasicId "a"
1200     fPar = AST.unsafeVHDLBasicId "f"
1201     sPar = AST.unsafeVHDLBasicId "s"
1202     resId   = AST.unsafeVHDLBasicId "res"    
1203     exSpec = AST.Function (mkVHDLExtId exId) [AST.IfaceVarDec vecPar vectorTM,
1204                                AST.IfaceVarDec ixPar  unsignedTM] elemTM
1205     exExpr = AST.ReturnSm (Just $ AST.PrimName $ AST.NIndexed 
1206               (AST.IndexedName (AST.NSimple vecPar) [genExprFCall (mkVHDLBasicId toIntegerId) (AST.PrimName $ AST.NSimple ixPar)]))
1207     replaceSpec = AST.Function (mkVHDLExtId replaceId)  [ AST.IfaceVarDec vecPar vectorTM
1208                                           , AST.IfaceVarDec iPar   unsignedTM
1209                                           , AST.IfaceVarDec aPar   elemTM
1210                                           ] vectorTM 
1211        -- variable res : fsvec_x (0 to vec'length-1);
1212     replaceVar =
1213          AST.VarDec resId 
1214                 (AST.SubtypeIn vectorTM
1215                   (Just $ AST.ConstraintIndex $ AST.IndexConstraint 
1216                    [AST.ToRange (AST.PrimLit "0")
1217                             (AST.PrimName (AST.NAttribute $ 
1218                               AST.AttribName (AST.NSimple vecPar) (AST.NSimple $ mkVHDLBasicId lengthId) Nothing) AST.:-:
1219                                 (AST.PrimLit "1"))   ]))
1220                 Nothing
1221        --  res AST.:= vec(0 to i-1) & a & vec(i+1 to length'vec-1)
1222     replaceExpr1 = AST.NSimple resId AST.:= AST.PrimName (AST.NSimple vecPar)
1223     replaceExpr2 = AST.NIndexed (AST.IndexedName (AST.NSimple resId) [genExprFCall (mkVHDLBasicId toIntegerId) (AST.PrimName $ AST.NSimple iPar)]) AST.:= AST.PrimName (AST.NSimple aPar)
1224     replaceRet =  AST.ReturnSm (Just $ AST.PrimName $ AST.NSimple resId)
1225     vecSlice init last =  AST.PrimName (AST.NSlice 
1226                                         (AST.SliceName 
1227                                               (AST.NSimple vecPar) 
1228                                               (AST.ToRange init last)))
1229     lastSpec = AST.Function (mkVHDLExtId lastId) [AST.IfaceVarDec vecPar vectorTM] elemTM
1230        -- return vec(vec'length-1);
1231     lastExpr = AST.ReturnSm (Just (AST.PrimName $ AST.NIndexed (AST.IndexedName 
1232                     (AST.NSimple vecPar) 
1233                     [AST.PrimName (AST.NAttribute $ 
1234                                 AST.AttribName (AST.NSimple vecPar) (AST.NSimple $ mkVHDLBasicId lengthId) Nothing) 
1235                                                              AST.:-: AST.PrimLit "1"])))
1236     initSpec = AST.Function (mkVHDLExtId initId) [AST.IfaceVarDec vecPar vectorTM] vectorTM 
1237        -- variable res : fsvec_x (0 to vec'length-2);
1238     initVar = 
1239          AST.VarDec resId 
1240                 (AST.SubtypeIn vectorTM
1241                   (Just $ AST.ConstraintIndex $ AST.IndexConstraint 
1242                    [AST.ToRange (AST.PrimLit "0")
1243                             (AST.PrimName (AST.NAttribute $ 
1244                               AST.AttribName (AST.NSimple vecPar) (AST.NSimple $ mkVHDLBasicId lengthId) Nothing) AST.:-:
1245                                 (AST.PrimLit "2"))   ]))
1246                 Nothing
1247        -- resAST.:= vec(0 to vec'length-2)
1248     initExpr = AST.NSimple resId AST.:= (vecSlice 
1249                                (AST.PrimLit "0") 
1250                                (AST.PrimName (AST.NAttribute $ 
1251                                   AST.AttribName (AST.NSimple vecPar) (AST.NSimple $ mkVHDLBasicId lengthId) Nothing) 
1252                                                              AST.:-: AST.PrimLit "2"))
1253     initRet =  AST.ReturnSm (Just $ AST.PrimName $ AST.NSimple resId)
1254     minimumSpec = AST.Function (mkVHDLExtId minimumId) [AST.IfaceVarDec leftPar   naturalTM,
1255                                    AST.IfaceVarDec rightPar naturalTM ] naturalTM
1256     minimumExpr = AST.IfSm ((AST.PrimName $ AST.NSimple leftPar) AST.:<: (AST.PrimName $ AST.NSimple rightPar))
1257                         [AST.ReturnSm (Just $ AST.PrimName $ AST.NSimple leftPar)]
1258                         []
1259                         (Just $ AST.Else [minimumExprRet])
1260       where minimumExprRet = AST.ReturnSm (Just $ AST.PrimName $ AST.NSimple rightPar)
1261     takeSpec = AST.Function (mkVHDLExtId takeId) [AST.IfaceVarDec nPar   naturalTM,
1262                                    AST.IfaceVarDec vecPar vectorTM ] vectorTM
1263        -- variable res : fsvec_x (0 to (minimum (n,vec'length))-1);
1264     minLength = AST.PrimFCall $ AST.FCall (AST.NSimple (mkVHDLExtId minimumId))  
1265                               [Nothing AST.:=>: AST.ADExpr (AST.PrimName $ AST.NSimple nPar)
1266                               ,Nothing AST.:=>: AST.ADExpr (AST.PrimName (AST.NAttribute $ 
1267                                 AST.AttribName (AST.NSimple vecPar) (AST.NSimple $ mkVHDLBasicId lengthId) Nothing))]
1268     takeVar = 
1269          AST.VarDec resId 
1270                 (AST.SubtypeIn vectorTM
1271                   (Just $ AST.ConstraintIndex $ AST.IndexConstraint 
1272                    [AST.ToRange (AST.PrimLit "0")
1273                                (minLength AST.:-:
1274                                 (AST.PrimLit "1"))   ]))
1275                 Nothing
1276        -- res AST.:= vec(0 to n-1)
1277     takeExpr = AST.NSimple resId AST.:= 
1278                     (vecSlice (AST.PrimLit "0") 
1279                               (minLength AST.:-: AST.PrimLit "1"))
1280     takeRet =  AST.ReturnSm (Just $ AST.PrimName $ AST.NSimple resId)
1281     dropSpec = AST.Function (mkVHDLExtId dropId) [AST.IfaceVarDec nPar   naturalTM,
1282                                    AST.IfaceVarDec vecPar vectorTM ] vectorTM 
1283        -- variable res : fsvec_x (0 to vec'length-n-1);
1284     dropVar = 
1285          AST.VarDec resId 
1286                 (AST.SubtypeIn vectorTM
1287                   (Just $ AST.ConstraintIndex $ AST.IndexConstraint 
1288                    [AST.ToRange (AST.PrimLit "0")
1289                             (AST.PrimName (AST.NAttribute $ 
1290                               AST.AttribName (AST.NSimple vecPar) (AST.NSimple $ mkVHDLBasicId lengthId) Nothing) AST.:-:
1291                                (AST.PrimName $ AST.NSimple nPar)AST.:-: (AST.PrimLit "1")) ]))
1292                Nothing
1293        -- res AST.:= vec(n to vec'length-1)
1294     dropExpr = AST.NSimple resId AST.:= (vecSlice 
1295                                (AST.PrimName $ AST.NSimple nPar) 
1296                                (AST.PrimName (AST.NAttribute $ 
1297                                   AST.AttribName (AST.NSimple vecPar) (AST.NSimple $ mkVHDLBasicId lengthId) Nothing) 
1298                                                              AST.:-: AST.PrimLit "1"))
1299     dropRet =  AST.ReturnSm (Just $ AST.PrimName $ AST.NSimple resId)
1300     plusgtSpec = AST.Function (mkVHDLExtId plusgtId) [AST.IfaceVarDec aPar   elemTM,
1301                                        AST.IfaceVarDec vecPar vectorTM] vectorTM 
1302     -- variable res : fsvec_x (0 to vec'length);
1303     plusgtVar = 
1304       AST.VarDec resId 
1305              (AST.SubtypeIn vectorTM
1306                (Just $ AST.ConstraintIndex $ AST.IndexConstraint 
1307                 [AST.ToRange (AST.PrimLit "0")
1308                         (AST.PrimName (AST.NAttribute $ 
1309                           AST.AttribName (AST.NSimple vecPar) (AST.NSimple $ mkVHDLBasicId lengthId) Nothing))]))
1310              Nothing
1311     plusgtExpr = AST.NSimple resId AST.:= 
1312                    ((AST.PrimName $ AST.NSimple aPar) AST.:&: 
1313                     (AST.PrimName $ AST.NSimple vecPar))
1314     plusgtRet = AST.ReturnSm (Just $ AST.PrimName $ AST.NSimple resId)
1315     emptySpec = AST.Function (mkVHDLExtId emptyId) [] vectorTM
1316     emptyVar = 
1317           AST.VarDec resId
1318             (AST.SubtypeIn vectorTM
1319               (Just $ AST.ConstraintIndex $ AST.IndexConstraint 
1320                 [AST.ToRange (AST.PrimLit "0") (AST.PrimLit "-1")]))
1321              Nothing
1322     emptyExpr = AST.ReturnSm (Just $ AST.PrimName (AST.NSimple resId))
1323     singletonSpec = AST.Function (mkVHDLExtId singletonId) [AST.IfaceVarDec aPar elemTM ] 
1324                                          vectorTM
1325     -- variable res : fsvec_x (0 to 0) := (others => a);
1326     singletonVar = 
1327       AST.VarDec resId 
1328              (AST.SubtypeIn vectorTM
1329                (Just $ AST.ConstraintIndex $ AST.IndexConstraint 
1330                 [AST.ToRange (AST.PrimLit "0") (AST.PrimLit "0")]))
1331              (Just $ AST.Aggregate [AST.ElemAssoc (Just AST.Others) 
1332                                           (AST.PrimName $ AST.NSimple aPar)])
1333     singletonRet = AST.ReturnSm (Just $ AST.PrimName $ AST.NSimple resId)
1334     copynSpec = AST.Function (mkVHDLExtId copynId) [AST.IfaceVarDec nPar   naturalTM,
1335                                    AST.IfaceVarDec aPar   elemTM   ] vectorTM 
1336     -- variable res : fsvec_x (0 to n-1) := (others => a);
1337     copynVar = 
1338       AST.VarDec resId 
1339              (AST.SubtypeIn vectorTM
1340                (Just $ AST.ConstraintIndex $ AST.IndexConstraint 
1341                 [AST.ToRange (AST.PrimLit "0")
1342                             ((AST.PrimName (AST.NSimple nPar)) AST.:-:
1343                              (AST.PrimLit "1"))   ]))
1344              (Just $ AST.Aggregate [AST.ElemAssoc (Just AST.Others) 
1345                                           (AST.PrimName $ AST.NSimple aPar)])
1346     -- return res
1347     copynExpr = AST.ReturnSm (Just $ AST.PrimName $ AST.NSimple resId)
1348     selSpec = AST.Function (mkVHDLExtId selId) [AST.IfaceVarDec fPar   naturalTM,
1349                                AST.IfaceVarDec sPar   naturalTM,
1350                                AST.IfaceVarDec nPar   naturalTM,
1351                                AST.IfaceVarDec vecPar vectorTM ] vectorTM
1352     -- variable res : fsvec_x (0 to n-1);
1353     selVar = 
1354       AST.VarDec resId 
1355                 (AST.SubtypeIn vectorTM
1356                   (Just $ AST.ConstraintIndex $ AST.IndexConstraint 
1357                     [AST.ToRange (AST.PrimLit "0")
1358                       ((AST.PrimName (AST.NSimple nPar)) AST.:-:
1359                       (AST.PrimLit "1"))   ])
1360                 )
1361                 Nothing
1362     -- for i res'range loop
1363     --   res(i) := vec(f+i*s);
1364     -- end loop;
1365     selFor = AST.ForSM iId (AST.AttribRange $ AST.AttribName (AST.NSimple resId) (AST.NSimple rangeId) Nothing) [selAssign]
1366     -- res(i) := vec(f+i*s);
1367     selAssign = let origExp = AST.PrimName (AST.NSimple fPar) AST.:+: 
1368                                 (AST.PrimName (AST.NSimple iId) AST.:*: 
1369                                   AST.PrimName (AST.NSimple sPar)) in
1370                                   AST.NIndexed (AST.IndexedName (AST.NSimple resId) [AST.PrimName (AST.NSimple iId)]) AST.:=
1371                                     (AST.PrimName $ AST.NIndexed (AST.IndexedName (AST.NSimple vecPar) [origExp]))
1372     -- return res;
1373     selRet =  AST.ReturnSm (Just $ AST.PrimName (AST.NSimple resId))
1374     ltplusSpec = AST.Function (mkVHDLExtId ltplusId) [AST.IfaceVarDec vecPar vectorTM,
1375                                         AST.IfaceVarDec aPar   elemTM] vectorTM 
1376      -- variable res : fsvec_x (0 to vec'length);
1377     ltplusVar = 
1378       AST.VarDec resId 
1379         (AST.SubtypeIn vectorTM
1380           (Just $ AST.ConstraintIndex $ AST.IndexConstraint 
1381             [AST.ToRange (AST.PrimLit "0")
1382               (AST.PrimName (AST.NAttribute $ 
1383                 AST.AttribName (AST.NSimple vecPar) (AST.NSimple $ mkVHDLBasicId lengthId) Nothing))]))
1384         Nothing
1385     ltplusExpr = AST.NSimple resId AST.:= 
1386                      ((AST.PrimName $ AST.NSimple vecPar) AST.:&: 
1387                       (AST.PrimName $ AST.NSimple aPar))
1388     ltplusRet = AST.ReturnSm (Just $ AST.PrimName $ AST.NSimple resId)
1389     plusplusSpec = AST.Function (mkVHDLExtId plusplusId) [AST.IfaceVarDec vec1Par vectorTM,
1390                                              AST.IfaceVarDec vec2Par vectorTM] 
1391                                              vectorTM 
1392     -- variable res : fsvec_x (0 to vec1'length + vec2'length -1);
1393     plusplusVar = 
1394       AST.VarDec resId 
1395         (AST.SubtypeIn vectorTM
1396           (Just $ AST.ConstraintIndex $ AST.IndexConstraint 
1397             [AST.ToRange (AST.PrimLit "0")
1398               (AST.PrimName (AST.NAttribute $ 
1399                 AST.AttribName (AST.NSimple vec1Par) (AST.NSimple $ mkVHDLBasicId lengthId) Nothing) AST.:+:
1400                   AST.PrimName (AST.NAttribute $ 
1401                 AST.AttribName (AST.NSimple vec2Par) (AST.NSimple $ mkVHDLBasicId lengthId) Nothing) AST.:-:
1402                   AST.PrimLit "1")]))
1403        Nothing
1404     plusplusExpr = AST.NSimple resId AST.:= 
1405                      ((AST.PrimName $ AST.NSimple vec1Par) AST.:&: 
1406                       (AST.PrimName $ AST.NSimple vec2Par))
1407     plusplusRet = AST.ReturnSm (Just $ AST.PrimName $ AST.NSimple resId)
1408     lengthTSpec = AST.Function (mkVHDLExtId lengthTId) [AST.IfaceVarDec vecPar vectorTM] naturalTM
1409     lengthTExpr = AST.ReturnSm (Just $ AST.PrimName (AST.NAttribute $ 
1410                                 AST.AttribName (AST.NSimple vecPar) (AST.NSimple $ mkVHDLBasicId lengthId) Nothing))
1411     shiftlSpec = AST.Function (mkVHDLExtId shiftlId) [AST.IfaceVarDec vecPar vectorTM,
1412                                    AST.IfaceVarDec aPar   elemTM  ] vectorTM 
1413     -- variable res : fsvec_x (0 to vec'length-1);
1414     shiftlVar = 
1415      AST.VarDec resId 
1416             (AST.SubtypeIn vectorTM
1417               (Just $ AST.ConstraintIndex $ AST.IndexConstraint 
1418                [AST.ToRange (AST.PrimLit "0")
1419                         (AST.PrimName (AST.NAttribute $ 
1420                           AST.AttribName (AST.NSimple vecPar) (AST.NSimple $ mkVHDLBasicId lengthId) Nothing) AST.:-:
1421                            (AST.PrimLit "1")) ]))
1422             Nothing
1423     -- res := a & init(vec)
1424     shiftlExpr = AST.NSimple resId AST.:=
1425                     (AST.PrimName (AST.NSimple aPar) AST.:&:
1426                      (AST.PrimFCall $ AST.FCall (AST.NSimple (mkVHDLExtId initId))  
1427                        [Nothing AST.:=>: AST.ADExpr (AST.PrimName $ AST.NSimple vecPar)]))
1428     shiftlRet = AST.ReturnSm (Just $ AST.PrimName $ AST.NSimple resId)       
1429     shiftrSpec = AST.Function (mkVHDLExtId shiftrId) [AST.IfaceVarDec vecPar vectorTM,
1430                                        AST.IfaceVarDec aPar   elemTM  ] vectorTM 
1431     -- variable res : fsvec_x (0 to vec'length-1);
1432     shiftrVar = 
1433      AST.VarDec resId 
1434             (AST.SubtypeIn vectorTM
1435               (Just $ AST.ConstraintIndex $ AST.IndexConstraint 
1436                [AST.ToRange (AST.PrimLit "0")
1437                         (AST.PrimName (AST.NAttribute $ 
1438                           AST.AttribName (AST.NSimple vecPar) (AST.NSimple $ mkVHDLBasicId lengthId) Nothing) AST.:-:
1439                            (AST.PrimLit "1")) ]))
1440             Nothing
1441     -- res := tail(vec) & a
1442     shiftrExpr = AST.NSimple resId AST.:=
1443                   ((AST.PrimFCall $ AST.FCall (AST.NSimple (mkVHDLExtId tailId))  
1444                     [Nothing AST.:=>: AST.ADExpr (AST.PrimName $ AST.NSimple vecPar)]) AST.:&:
1445                   (AST.PrimName (AST.NSimple aPar)))
1446                 
1447     shiftrRet = AST.ReturnSm (Just $ AST.PrimName $ AST.NSimple resId)      
1448     nullSpec = AST.Function (mkVHDLExtId nullId) [AST.IfaceVarDec vecPar vectorTM] booleanTM
1449     -- return vec'length = 0
1450     nullExpr = AST.ReturnSm (Just $ 
1451                 AST.PrimName (AST.NAttribute $ 
1452                   AST.AttribName (AST.NSimple vecPar) (AST.NSimple $ mkVHDLBasicId lengthId) Nothing) AST.:=:
1453                     AST.PrimLit "0")
1454     rotlSpec = AST.Function (mkVHDLExtId rotlId) [AST.IfaceVarDec vecPar vectorTM] vectorTM 
1455     -- variable res : fsvec_x (0 to vec'length-1);
1456     rotlVar = 
1457      AST.VarDec resId 
1458             (AST.SubtypeIn vectorTM
1459               (Just $ AST.ConstraintIndex $ AST.IndexConstraint 
1460                [AST.ToRange (AST.PrimLit "0")
1461                         (AST.PrimName (AST.NAttribute $ 
1462                           AST.AttribName (AST.NSimple vecPar) (AST.NSimple $ mkVHDLBasicId lengthId) Nothing) AST.:-:
1463                            (AST.PrimLit "1")) ]))
1464             Nothing
1465     -- if null(vec) then res := vec else res := last(vec) & init(vec)
1466     rotlExpr = AST.IfSm (AST.PrimFCall $ AST.FCall (AST.NSimple (mkVHDLExtId nullId))  
1467                           [Nothing AST.:=>: AST.ADExpr (AST.PrimName $ AST.NSimple vecPar)])
1468                         [AST.NSimple resId AST.:= (AST.PrimName $ AST.NSimple vecPar)]
1469                         []
1470                         (Just $ AST.Else [rotlExprRet])
1471       where rotlExprRet = 
1472                 AST.NSimple resId AST.:= 
1473                       ((AST.PrimFCall $ AST.FCall (AST.NSimple (mkVHDLExtId lastId))  
1474                         [Nothing AST.:=>: AST.ADExpr (AST.PrimName $ AST.NSimple vecPar)]) AST.:&:
1475                       (AST.PrimFCall $ AST.FCall (AST.NSimple (mkVHDLExtId initId))  
1476                         [Nothing AST.:=>: AST.ADExpr (AST.PrimName $ AST.NSimple vecPar)]))
1477     rotlRet =  AST.ReturnSm (Just $ AST.PrimName $ AST.NSimple resId)       
1478     rotrSpec = AST.Function (mkVHDLExtId rotrId) [AST.IfaceVarDec vecPar vectorTM] vectorTM 
1479     -- variable res : fsvec_x (0 to vec'length-1);
1480     rotrVar = 
1481      AST.VarDec resId 
1482             (AST.SubtypeIn vectorTM
1483               (Just $ AST.ConstraintIndex $ AST.IndexConstraint 
1484                [AST.ToRange (AST.PrimLit "0")
1485                         (AST.PrimName (AST.NAttribute $ 
1486                           AST.AttribName (AST.NSimple vecPar) (AST.NSimple $ mkVHDLBasicId lengthId) Nothing) AST.:-:
1487                            (AST.PrimLit "1")) ]))
1488             Nothing
1489     -- if null(vec) then res := vec else res := tail(vec) & head(vec)
1490     rotrExpr = AST.IfSm (AST.PrimFCall $ AST.FCall (AST.NSimple (mkVHDLExtId nullId))  
1491                           [Nothing AST.:=>: AST.ADExpr (AST.PrimName $ AST.NSimple vecPar)])
1492                         [AST.NSimple resId AST.:= (AST.PrimName $ AST.NSimple vecPar)]
1493                         []
1494                         (Just $ AST.Else [rotrExprRet])
1495       where rotrExprRet = 
1496                 AST.NSimple resId AST.:= 
1497                       ((AST.PrimFCall $ AST.FCall (AST.NSimple (mkVHDLExtId tailId))  
1498                         [Nothing AST.:=>: AST.ADExpr (AST.PrimName $ AST.NSimple vecPar)]) AST.:&:
1499                       (AST.PrimFCall $ AST.FCall (AST.NSimple (mkVHDLExtId headId))  
1500                         [Nothing AST.:=>: AST.ADExpr (AST.PrimName $ AST.NSimple vecPar)]))
1501     rotrRet =  AST.ReturnSm (Just $ AST.PrimName $ AST.NSimple resId)
1502     reverseSpec = AST.Function (mkVHDLExtId reverseId) [AST.IfaceVarDec vecPar vectorTM] vectorTM
1503     reverseVar = 
1504       AST.VarDec resId 
1505              (AST.SubtypeIn vectorTM
1506                (Just $ AST.ConstraintIndex $ AST.IndexConstraint 
1507                 [AST.ToRange (AST.PrimLit "0")
1508                          (AST.PrimName (AST.NAttribute $ 
1509                            AST.AttribName (AST.NSimple vecPar) (AST.NSimple $ mkVHDLBasicId lengthId) Nothing) AST.:-:
1510                             (AST.PrimLit "1")) ]))
1511              Nothing
1512     -- for i in 0 to res'range loop
1513     --   res(vec'length-i-1) := vec(i);
1514     -- end loop;
1515     reverseFor = 
1516        AST.ForSM iId (AST.AttribRange $ AST.AttribName (AST.NSimple resId) (AST.NSimple rangeId) Nothing) [reverseAssign]
1517     -- res(vec'length-i-1) := vec(i);
1518     reverseAssign = AST.NIndexed (AST.IndexedName (AST.NSimple resId) [destExp]) AST.:=
1519       (AST.PrimName $ AST.NIndexed (AST.IndexedName (AST.NSimple vecPar) 
1520                            [AST.PrimName $ AST.NSimple iId]))
1521         where destExp = AST.PrimName (AST.NAttribute $ AST.AttribName (AST.NSimple vecPar) 
1522                                    (AST.NSimple $ mkVHDLBasicId lengthId) Nothing) AST.:-: 
1523                         AST.PrimName (AST.NSimple iId) AST.:-: 
1524                         (AST.PrimLit "1") 
1525     -- return res;
1526     reverseRet = AST.ReturnSm (Just $ AST.PrimName (AST.NSimple resId))
1527
1528     
1529 -----------------------------------------------------------------------------
1530 -- A table of builtin functions
1531 -----------------------------------------------------------------------------
1532
1533 -- A function that generates VHDL for a builtin function
1534 type BuiltinBuilder = 
1535   (Either CoreSyn.CoreBndr AST.VHDLName) -- ^ The destination signal and it's original type
1536   -> CoreSyn.CoreBndr -- ^ The function called
1537   -> [Either CoreSyn.CoreExpr AST.Expr] -- ^ The value arguments passed (excluding type and
1538                     --   dictionary arguments).
1539   -> TranslatorSession ([AST.ConcSm], [CoreSyn.CoreBndr]) 
1540   -- ^ The corresponding VHDL concurrent statements and entities
1541   --   instantiated.
1542
1543 -- A map of a builtin function to VHDL function builder 
1544 type NameTable = Map.Map String (Int, BuiltinBuilder )
1545
1546 -- | The builtin functions we support. Maps a name to an argument count and a
1547 -- builder function.
1548 globalNameTable :: NameTable
1549 globalNameTable = Map.fromList
1550   [ (exId             , (2, genFCall True          ) )
1551   , (replaceId        , (3, genFCall False          ) )
1552   , (headId           , (1, genFCall True           ) )
1553   , (lastId           , (1, genFCall True           ) )
1554   , (tailId           , (1, genFCall False          ) )
1555   , (initId           , (1, genFCall False          ) )
1556   , (takeId           , (2, genFCall False          ) )
1557   , (dropId           , (2, genFCall False          ) )
1558   , (selId            , (4, genFCall False          ) )
1559   , (plusgtId         , (2, genFCall False          ) )
1560   , (ltplusId         , (2, genFCall False          ) )
1561   , (plusplusId       , (2, genFCall False          ) )
1562   , (mapId            , (2, genMap                  ) )
1563   , (zipWithId        , (3, genZipWith              ) )
1564   , (foldlId          , (3, genFoldl                ) )
1565   , (foldrId          , (3, genFoldr                ) )
1566   , (zipId            , (2, genZip                  ) )
1567   , (unzipId          , (1, genUnzip                ) )
1568   , (shiftlId         , (2, genFCall False          ) )
1569   , (shiftrId         , (2, genFCall False          ) )
1570   , (rotlId           , (1, genFCall False          ) )
1571   , (rotrId           , (1, genFCall False          ) )
1572   , (concatId         , (1, genConcat               ) )
1573   , (reverseId        , (1, genFCall False          ) )
1574   , (iteratenId       , (3, genIteraten             ) )
1575   , (iterateId        , (2, genIterate              ) )
1576   , (generatenId      , (3, genGeneraten            ) )
1577   , (generateId       , (2, genGenerate             ) )
1578   , (emptyId          , (0, genFCall False          ) )
1579   , (singletonId      , (1, genFCall False          ) )
1580   , (copynId          , (2, genFCall False          ) )
1581   , (copyId           , (1, genCopy                 ) )
1582   , (lengthTId        , (1, genFCall False          ) )
1583   , (nullId           , (1, genFCall False          ) )
1584   , (hwxorId          , (2, genOperator2 AST.Xor    ) )
1585   , (hwandId          , (2, genOperator2 AST.And    ) )
1586   , (hworId           , (2, genOperator2 AST.Or     ) )
1587   , (hwnotId          , (1, genOperator1 AST.Not    ) )
1588   , (equalityId       , (2, genOperator2 (AST.:=:)  ) )
1589   , (inEqualityId     , (2, genOperator2 (AST.:/=:) ) )
1590   , (ltId             , (2, genOperator2 (AST.:<:)  ) )
1591   , (lteqId           , (2, genOperator2 (AST.:<=:) ) )
1592   , (gtId             , (2, genOperator2 (AST.:>:)  ) )
1593   , (gteqId           , (2, genOperator2 (AST.:>=:) ) )
1594   , (boolOrId         , (2, genOperator2 AST.Or     ) )
1595   , (boolAndId        , (2, genOperator2 AST.And    ) )
1596   , (plusId           , (2, genOperator2 (AST.:+:)  ) )
1597   , (timesId          , (2, genTimes                ) )
1598   , (negateId         , (1, genNegation             ) )
1599   , (minusId          , (2, genOperator2 (AST.:-:)  ) )
1600   , (fromSizedWordId  , (1, genFromSizedWord        ) )
1601   , (fromIntegerId    , (1, genFromInteger          ) )
1602   , (resizeWordId     , (1, genResize               ) )
1603   , (resizeIntId      , (1, genResize               ) )
1604   , (sizedIntId       , (1, genSizedInt             ) )
1605   , (smallIntegerId   , (1, genFromInteger          ) )
1606   , (fstId            , (1, genFst                  ) )
1607   , (sndId            , (1, genSnd                  ) )
1608   , (blockRAMId       , (5, genBlockRAM             ) )
1609   , (splitId          , (1, genSplit                ) )
1610   --, (tfvecId          , (1, genTFVec                ) )
1611   , (minimumId        , (2, error "\nFunction name: \"minimum\" is used internally, use another name"))
1612   ]