Add another reference.
[matthijs/master-project/report.git] / Chapters / Prototype.tex
1 \chapter[chap:prototype]{Prototype}
2   An important step in this research is the creation of a prototype compiler.
3   Having this prototype allows us to apply the ideas from the previous chapter
4   to actual hardware descriptions and evaluate their usefulness. Having a
5   prototype also helps to find new techniques and test possible
6   interpretations.
7
8   Obviously the prototype was not created after all research
9   ideas were formed, but its implementation has been interleaved with the
10   research itself. Also, the prototype described here is the final version, it
11   has gone through a number of design iterations which we will not completely
12   describe here.
13
14   \section[sec:prototype:input]{Input language}
15     When implementing this prototype, the first question to ask is: What
16     (functional) language will we use to describe our hardware? (Note that
17     this does not concern the \emph{implementation language} of the compiler,
18     just the language \emph{translated by} the compiler).
19
20     Initially, we have two choices:
21
22     \startitemize
23       \item Create a new functional language from scratch. This has the
24       advantage of having a language that contains exactly those elements that
25       are convenient for describing hardware and can contain special
26       constructs that allows our hardware descriptions to be more powerful or
27       concise.
28       \item Use an existing language and create a new backend for it. This has
29       the advantage that existing tools can be reused, which will speed up
30       development.
31     \stopitemize
32
33
34     \placeintermezzo{}{
35       \startframedtext[width=8cm,background=box,frame=no]
36       \startalignment[center]
37         {\tfa No \small{EDSL} or Template Haskell}
38       \stopalignment
39       \blank[medium]
40
41       Note that in this consideration, embedded domain-specific
42       languages (\small{EDSL}) and Template Haskell (\small{TH})
43       approaches have not been included. As we've seen in
44       \in{section}[sec:context:fhdls], these approaches have all kinds
45       of limitations on the description language that we would like to
46       avoid.
47       \stopframedtext
48     }
49     Considering that we required a prototype which should be working quickly,
50     and that implementing parsers, semantic checkers and especially
51     typcheckers is not exactly the core of this research (but it is lots and
52     lots of work!), using an existing language is the obvious choice. This
53     also has the advantage that a large set of language features is available
54     to experiment with and it is easy to find which features apply well and
55     which don't. A possible second prototype could use a custom language with
56     just the useful features (and possibly extra features that are specific to
57     the domain of hardware description as well).
58
59     The second choice is which of the many existing languages to use. As
60     mentioned before, the chosen language is Haskell.  This choice has not been the
61     result of a thorough comparison of languages, for the simple reason that
62     the requirements on the language were completely unclear at the start of
63     this research. The fact that Haskell is a language with a broad spectrum
64     of features, that it is commonly used in research projects and that the
65     primary compiler, \GHC, provides a high level API to its internals, made
66     Haskell an obvious choice.
67
68   \section[sec:prototype:output]{Output format}
69     The second important question is: What will be our output format? Since
70     our prototype won't be able to program FPGA's directly, we'll have to have
71     output our hardware in some format that can be later processed and
72     programmed by other tools.
73
74     Looking at other tools in the industry, the Electronic Design Interchange
75     Format (\small{EDIF}) is commonly used for storing intermediate
76     \emph{netlists} (lists of components and connections between these
77     components) and is commonly the target for \small{VHDL} and Verilog
78     compilers.
79
80     However, \small{EDIF} is not completely tool-independent. It specifies a
81     meta-format, but the hardware components that can be used vary between
82     various tool and hardware vendors, as well as the interpretation of the
83     \small{EDIF} standard. \cite[li89]
84    
85     This means that when working with \small{EDIF}, our prototype would become
86     technology dependent (\eg only work with \small{FPGA}s of a specific
87     vendor, or even only with specific chips). This limits the applicability
88     of our prototype. Also, the tools we'd like to use for verifying,
89     simulating and draw pretty pictures of our output (like Precision, or
90     QuestaSim) are designed for \small{VHDL} or Verilog input.
91
92     For these reasons, we will not use \small{EDIF}, but \small{VHDL} as our
93     output language.  We choose \VHDL over Verilog simply because we are
94     familiar with \small{VHDL} already. The differences between \small{VHDL}
95     and Verilog are on the higher level, while we will be using \small{VHDL}
96     mainly to write low level, netlist-like descriptions anyway.
97
98     An added advantage of using VHDL is that we can profit from existing
99     optimizations in VHDL synthesizers. A lot of optimizations are done on the
100     VHDL level by existing tools. These tools have years of experience in this
101     field, so it would not be reasonable to assume we could achieve a similar
102     amount of optimization in our prototype (nor should it be a goal,
103     considering this is just a prototype).
104
105     Note that we will be using \small{VHDL} as our output language, but will
106     not use its full expressive power. Our output will be limited to using
107     simple, structural descriptions, without any behavioural descriptions
108     (which might not be supported by all tools). This ensures that any tool
109     that works with \VHDL will understand our output (most tools don't support
110     synthesis of more complex \VHDL). This also leaves open the option to
111     switch to \small{EDIF} in the future, with minimal changes to the
112     prototype.
113
114   \section[sec:prototype:design]{Prototype design}
115     As suggested above, we will use the Glasgow Haskell Compiler (\small{GHC}) to
116     implement our prototype compiler. To understand the design of the
117     compiler, we will first dive into the \small{GHC} compiler a bit. It's
118     compilation consists of the following steps (slightly simplified):
119
120     \startuseMPgraphic{ghc-pipeline}
121       % Create objects
122       save inp, front, desugar, simpl, back, out;
123       newEmptyBox.inp(0,0);
124       newBox.front(btex Fronted etex);
125       newBox.desugar(btex Desugarer etex);
126       newBox.simpl(btex Simplifier etex);
127       newBox.back(btex Backend etex);
128       newEmptyBox.out(0,0);
129
130       % Space the boxes evenly
131       inp.c - front.c = front.c - desugar.c = desugar.c - simpl.c 
132         = simpl.c - back.c = back.c - out.c = (0, 1.5cm);
133       out.c = origin;
134
135       % Draw lines between the boxes. We make these lines "deferred" and give
136       % them a name, so we can use ObjLabel to draw a label beside them.
137       ncline.inp(inp)(front) "name(haskell)";
138       ncline.front(front)(desugar) "name(ast)";
139       ncline.desugar(desugar)(simpl) "name(core)";
140       ncline.simpl(simpl)(back) "name(simplcore)";
141       ncline.back(back)(out) "name(native)";
142       ObjLabel.inp(btex Haskell source etex) "labpathname(haskell)", "labdir(rt)";
143       ObjLabel.front(btex Haskell AST etex) "labpathname(ast)", "labdir(rt)";
144       ObjLabel.desugar(btex Core etex) "labpathname(core)", "labdir(rt)";
145       ObjLabel.simpl(btex Simplified core etex) "labpathname(simplcore)", "labdir(rt)";
146       ObjLabel.back(btex Native code etex) "labpathname(native)", "labdir(rt)";
147
148       % Draw the objects (and deferred labels)
149       drawObj (inp, front, desugar, simpl, back, out);
150     \stopuseMPgraphic
151     \placefigure[right]{GHC compiler pipeline}{\useMPgraphic{ghc-pipeline}}
152
153     \startdesc{Frontend}
154       This step takes the Haskell source files and parses them into an
155       abstract syntax tree (\small{AST}). This \small{AST} can express the
156       complete Haskell language and is thus a very complex one (in contrast
157       with the Core \small{AST}, later on). All identifiers in this
158       \small{AST} are resolved by the renamer and all types are checked by the
159       typechecker.
160     \stopdesc
161     \startdesc{Desugaring}
162       This steps takes the full \small{AST} and translates it to the
163       \emph{Core} language. Core is a very small functional language with lazy
164       semantics, that can still express everything Haskell can express. Its
165       simpleness makes Core very suitable for further simplification and
166       translation. Core is the language we will be working with as well.
167     \stopdesc
168     \startdesc{Simplification}
169       Through a number of simplification steps (such as inlining, common
170       subexpression elimination, etc.) the Core program is simplified to make
171       it faster or easier to process further.
172     \stopdesc
173     \startdesc{Backend}
174       This step takes the simplified Core program and generates an actual
175       runnable program for it. This is a big and complicated step we will not
176       discuss it any further, since it is not required for our prototype.
177     \stopdesc
178
179     In this process, there a number of places where we can start our work.
180     Assuming that we don't want to deal with (or modify) parsing, typechecking
181     and other frontend business and that native code isn't really a useful
182     format anymore, we are left with the choice between the full Haskell
183     \small{AST}, or the smaller (simplified) core representation.
184
185     The advantage of taking the full \small{AST} is that the exact structure
186     of the source program is preserved. We can see exactly what the hardware
187     descriiption looks like and which syntax constructs were used. However,
188     the full \small{AST} is a very complicated datastructure. If we are to
189     handle everything it offers, we will quickly get a big compiler.
190
191     Using the core representation gives us a much more compact datastructure
192     (a core expression only uses 9 constructors). Note that this does not mean
193     that the core representation itself is smaller, on the contrary. Since the
194     core language has less constructs, a lot of things will take a larger
195     expression to express.
196
197     However, the fact that the core language is so much smaller, means it is a
198     lot easier to analyze and translate it into something else. For the same
199     reason, \small{GHC} runs its simplifications and optimizations on the core
200     representation as well.
201
202     However, we will use the normal core representation, not the simplified
203     core. Reasons for this are detailed below. \todo{Ref}
204     
205     The final prototype roughly consists of three steps:
206     
207     \startuseMPgraphic{clash-pipeline}
208       % Create objects
209       save inp, front, norm, vhdl, out;
210       newEmptyBox.inp(0,0);
211       newBox.front(btex \small{GHC} frontend + desugarer etex);
212       newBox.norm(btex Normalization etex);
213       newBox.vhdl(btex \small{VHDL} generation etex);
214       newEmptyBox.out(0,0);
215
216       % Space the boxes evenly
217       inp.c - front.c = front.c - norm.c = norm.c - vhdl.c 
218         = vhdl.c - out.c = (0, 1.5cm);
219       out.c = origin;
220
221       % Draw lines between the boxes. We make these lines "deferred" and give
222       % them a name, so we can use ObjLabel to draw a label beside them.
223       ncline.inp(inp)(front) "name(haskell)";
224       ncline.front(front)(norm) "name(core)";
225       ncline.norm(norm)(vhdl) "name(normal)";
226       ncline.vhdl(vhdl)(out) "name(vhdl)";
227       ObjLabel.inp(btex Haskell source etex) "labpathname(haskell)", "labdir(rt)";
228       ObjLabel.front(btex Core etex) "labpathname(core)", "labdir(rt)";
229       ObjLabel.norm(btex Normalized core etex) "labpathname(normal)", "labdir(rt)";
230       ObjLabel.vhdl(btex \small{VHDL} description etex) "labpathname(vhdl)", "labdir(rt)";
231
232       % Draw the objects (and deferred labels)
233       drawObj (inp, front, norm, vhdl, out);
234     \stopuseMPgraphic
235     \placefigure[right]{Cλash compiler pipeline}{\useMPgraphic{clash-pipeline}}
236
237     \startdesc{Frontend}
238       This is exactly the frontend and desugarer from the \small{GHC}
239       pipeline, that translates Haskell sources to a core representation.
240     \stopdesc
241     \startdesc{Normalization}
242       This is a step that transforms the core representation into a normal
243       form. This normal form is still expressed in the core language, but has
244       to adhere to an extra set of constraints. This normal form is less
245       expressive than the full core language (e.g., it can have limited higher
246       order expressions, has a specific structure, etc.), but is also very
247       close to directly describing hardware.
248     \stopdesc
249     \startdesc{\small{VHDL} generation}
250       The last step takes the normal formed core representation and generates
251       \small{VHDL} for it. Since the normal form has a specific, hardware-like
252       structure, this final step is very straightforward.
253     \stopdesc
254     
255     The most interesting step in this process is the normalization step. That
256     is where more complicated functional constructs, which have no direct
257     hardware interpretation, are removed and translated into hardware
258     constructs. This step is described in a lot of detail at
259     \in{chapter}[chap:normalization].
260     
261   \section{The Core language}
262     \defreftxt{core}{the Core language}
263     Most of the prototype deals with handling the program in the Core
264     language. In this section we will show what this language looks like and
265     how it works.
266
267     The Core language is a functional language that describes
268     \emph{expressions}. Every identifier used in Core is called a
269     \emph{binder}, since it is bound to a value somewhere. On the highest
270     level, a Core program is a collection of functions, each of which bind a
271     binder (the function name) to an expression (the function value, which has
272     a function type).
273
274     The Core language itself does not prescribe any program structure, only
275     expression structure. In the \small{GHC} compiler, the Haskell module
276     structure is used for the resulting Core code as well. Since this is not
277     so relevant for understanding the Core language or the Normalization
278     process, we'll only look at the Core expression language here.
279
280     Each Core expression consists of one of these possible expressions.
281
282     \startdesc{Variable reference}
283       \defref{variable reference}
284       \startlambda
285       x :: T
286       \stoplambda
287       This is a reference to a binder. It's written down as the
288       name of the binder that is being referred to along with its type. The
289       binder name should of course be bound in a containing scope (including
290       top level scope, so a reference to a top level function is also a
291       variable reference). Additionally, constructors from algebraic datatypes
292       also become variable references.
293
294       The value of this expression is the value bound to the given binder.
295
296       Each binder also carries around its type (explicitly shown above), but
297       this is usually not shown in the Core expressions. Only when the type is
298       relevant (when a new binder is introduced, for example) will it be
299       shown. In other cases, the binder is either not relevant, or easily
300       derived from the context of the expression. \todo{Ref sidenote on type
301       annotations}
302     \stopdesc
303
304     \startdesc{Literal}
305       \defref{literal}
306       \startlambda
307       10
308       \stoplambda
309       This is a literal. Only primitive types are supported, like
310       chars, strings, ints and doubles. The types of these literals are the
311       \quote{primitive} versions, like \lam{Char\#} and \lam{Word\#}, not the
312       normal Haskell versions (but there are builtin conversion functions).
313     \stopdesc
314
315     \startdesc{Application}
316       \defref{application}
317       \startlambda
318       func arg
319       \stoplambda
320       This is function application. Each application consists of two
321       parts: The function part and the argument part. Applications are used
322       for normal function \quote{calls}, but also for applying type
323       abstractions and data constructors.
324       
325       The value of an application is the value of the function part, with the
326       first argument binder bound to the argument part.
327     \stopdesc
328
329     \startdesc{Lambda abstraction}
330       \defref{lambda abstraction}
331       \startlambda
332       λbndr.body
333       \stoplambda
334       This is the basic lambda abstraction, as it occurs in labmda calculus.
335       It consists of a binder part and a body part.  A lambda abstraction
336       creates a function, that can be applied to an argument. The binder is
337       usually a value binder, but it can also be a \emph{type binder} (or
338       \emph{type variable}). The latter case introduces a new polymorphic
339       variable, which can be used in types later on. See
340       \in{section}[sec:prototype:coretypes] for details.
341      
342       Note that the body of a lambda abstraction extends all the way to the
343       end of the expression, or the closing bracket surrounding the lambda. In
344       other words, the lambda abstraction \quote{operator} has the lowest
345       priority of all.
346
347       The value of an application is the value of the body part, with the
348       binder bound to the value the entire lambda abstraction is applied to.
349     \stopdesc
350
351     \startdesc{Non-recursive let expression}
352       \defref{let expression}
353       \startlambda
354       let bndr = value in body
355       \stoplambda
356       A let expression allows you to bind a binder to some value, while
357       evaluating to some other value (where that binder is in scope). This
358       allows for sharing of subexpressions (you can use a binder twice) and
359       explicit \quote{naming} of arbitrary expressions. Note that the binder
360       is not in scope in the value bound to it, so it's not possible to make
361       recursive definitions with the normal form of the let expression (see
362       the recursive form below).
363
364       Even though this let expression is an extension on the basic lambda
365       calculus, it is easily translated to a lambda abstraction. The let
366       expression above would then become:
367
368       \startlambda
369       (λbndr.body) value
370       \stoplambda
371
372       This notion might be useful for verifying certain properties on
373       transformations, since a lot of verification work has been done on
374       lambda calculus already.
375
376       The value of a let expression is the value of the body part, with the
377       binder bound to the value. 
378     \stopdesc
379
380     \startdesc{Recursive let expression}
381       \startlambda
382       letrec
383         bndr1 = value1
384         \vdots
385         bndrn = valuen
386       in 
387         body
388       \stoplambda
389       This is the recursive version of the let expression. In \small{GHC}'s
390       Core implementation, non-recursive and recursive lets are not so
391       distinct as we present them here, but this provides a clearer overview.
392       
393       The main difference with the normal let expression is that each of the
394       binders is in scope in each of the values, in addition to the body. This
395       allows for self-recursive or mutually recursive definitions.
396
397       It should also be possible to express a recursive let using normal
398       lambda calculus, if we use the \emph{least fixed-point operator},
399       \lam{Y}. This falls beyond the scope of this report, since it is not
400       needed for this research.
401     \stopdesc
402
403     \startdesc{Case expression}
404       \defref{case expression}
405       \startlambda
406         case scrutinee of bndr
407           DEFAULT -> defaultbody
408           C0 bndr0,0 ... bndr0,m -> body0
409           \vdots
410           Cn bndrn,0 ... bndrn,m -> bodyn
411       \stoplambda
412
413       \todo{Define WHNF}
414
415       A case expression is the only way in Core to choose between values. All
416       \hs{if} expressions and pattern matchings from the original Haskell
417       PRogram have been translated to case expressions by the desugarer. 
418       
419       A case expression evaluates its scrutinee, which should have an
420       algebraic datatype, into weak head normal form (\small{WHNF}) and
421       (optionally) binds it to \lam{bndr}. It then chooses a body depending on
422       the constructor of its scrutinee. If none of the constructors match, the
423       \lam{DEFAULT} alternative is chosen. A case expression must always be
424       exhaustive, \ie it must cover all possible constructors that the
425       scrutinee can have (if all of them are covered explicitly, the
426       \lam{DEFAULT} alternative can be left out).
427       
428       Since we can only match the top level constructor, there can be no overlap
429       in the alternatives and thus order of alternatives is not relevant (though
430       the \lam{DEFAULT} alternative must appear first for implementation
431       efficiency).
432       
433       Any arguments to the constructor in the scrutinee are bound to each of the
434       binders after the constructor and are in scope only in the corresponding
435       body.
436
437       To support strictness, the scrutinee is always evaluated into
438       \small{WHNF}, even when there is only a \lam{DEFAULT} alternative. This
439       allows aplication of the strict function \lam{f} to the argument \lam{a}
440       to be written like:
441
442       \startlambda
443       f (case a of arg DEFAULT -> arg)
444       \stoplambda
445
446       According to the \GHC documentation, this is the only use for the extra
447       binder to which the scrutinee is bound.  When not using strictness
448       annotations (which is rather pointless in hardware descriptions),
449       \small{GHC} seems to never generate any code making use of this binder.
450       In fact, \GHC has never been observed to generate code using this
451       binder, even when strictness was involved.  Nonetheless, the prototype
452       handles this binder as expected.
453
454       Note that these case statements are less powerful than the full Haskell
455       case statements. In particular, they do not support complex patterns like
456       in Haskell. Only the constructor of an expression can be matched,
457       complex patterns are implemented using multiple nested case expressions.
458
459       Case statements are also used for unpacking of algebraic datatypes, even
460       when there is only a single constructor. For examples, to add the elements
461       of a tuple, the following Core is generated:
462
463       \startlambda
464       sum = λtuple.case tuple of
465         (,) a b -> a + b
466       \stoplambda
467     
468       Here, there is only a single alternative (but no \lam{DEFAULT}
469       alternative, since the single alternative is already exhaustive). When
470       it's body is evaluated, the arguments to the tuple constructor \lam{(,)}
471       (\eg, the elements of the tuple) are bound to \lam{a} and \lam{b}.
472     \stopdesc
473
474     \startdesc{Cast expression}
475       \defref{cast expression}
476       \startlambda
477       body ▶ targettype
478       \stoplambda
479       A cast expression allows you to change the type of an expression to an
480       equivalent type. Note that this is not meant to do any actual work, like
481       conversion of data from one format to another, or force a complete type
482       change. Instead, it is meant to change between different representations
483       of the same type, \eg switch between types that are provably equal (but
484       look different).
485       
486       In our hardware descriptions, we typically see casts to change between a
487       Haskell newtype and its contained type, since those are effectively
488       different types (so a cast is needed) with the same representation (but
489       no work is done by the cast).
490
491       More complex are types that are proven to be equal by the typechecker,
492       but look different at first glance. To ensure that, once the typechecker
493       has proven equality, this information sticks around, explicit casts are
494       added. In our notation we only write the target type, but in reality a
495       cast expressions carries around a \emph{coercion}, which can be seen as a
496       proof of equality. \todo{Example}
497
498       The value of a cast is the value of its body, unchanged. The type of this
499       value is equal to the target type, not the type of its body.
500
501       \todo{Move and update this paragraph}
502       Note that this syntax is also used sometimes to indicate that a particular
503       expression has a particular type, even when no cast expression is
504       involved. This is then purely informational, since the only elements that
505       are explicitely typed in the Core language are the binder references and
506       cast expressions, the types of all other elements are determined at
507       runtime.
508     \stopdesc
509
510     \startdesc{Note}
511       The Core language in \small{GHC} allows adding \emph{notes}, which serve
512       as hints to the inliner or add custom (string) annotations to a core
513       expression. These shouldn't be generated normally, so these are not
514       handled in any way in the prototype.
515     \stopdesc
516
517     \startdesc{Type}
518       \defref{type expression}
519       \startlambda
520       @type
521       \stoplambda
522       It is possibly to use a Core type as a Core expression. For the actual
523       types supported by Core, see \in{section}[sec:prototype:coretypes]. This
524       \quote{lifting} of a type into the value domain is done to allow for
525       type abstractions and applications to be handled as normal lambda
526       abstractions and applications above. This means that a type expression
527       in Core can only ever occur in the argument position of an application,
528       and only if the type of the function that is applied to expects a type
529       as the first argument. This happens for all polymorphic functions, for
530       example, the \lam{fst} function:
531
532       \startlambda
533       fst :: \forall a. \forall b. (a, b) -> a
534       fst = λtup.case tup of (,) a b -> a
535
536       fstint :: (Int, Int) -> Int
537       fstint = λa.λb.fst @Int @Int a b
538       \stoplambda
539           
540       The type of \lam{fst} has two universally quantified type variables. When
541       \lam{fst} is applied in \lam{fstint}, it is first applied to two types.
542       (which are substitued for \lam{a} and \lam{b} in the type of \lam{fst}, so
543       the type of \lam{fst} actual type of arguments and result can be found:
544       \lam{fst @Int @Int :: (Int, Int) -> Int}).
545     \stopdesc
546
547     \subsection[sec:prototype:coretypes]{Core type system}
548       Whereas the expression syntax of Core is very simple, its type system is
549       a bit more complicated. It turns out it is harder to \quote{desugar}
550       Haskell's complex type system into something more simple. Most of the
551       type system is thus very similar to that of Haskell.
552
553       We will slightly limit our view on Core's type system, since the more
554       complicated parts of it are only meant to support Haskell's (or rather,
555       \GHC's) type extensions, such as existential types, \small{GADT}s, type
556       families and other non-standard Haskell stuff which we don't (plan to)
557       support.
558
559       In Core, every expression is typed. The translation to Core happens
560       after the typechecker, so types in Core are always correct as well
561       (though you could of course construct invalidly typed expressions).
562
563       Any type in core is one of the following:
564
565       \startdesc{A type variable}
566         \startlambda
567         t
568         \stoplambda
569
570         This is a reference to a type defined elsewhere. This can either be a
571         polymorphic type (like the latter two \lam{t}'s in \lam{id :: \forall t.
572         t -> t}), or a type constructor (like \lam{Bool} in \lam{not :: Bool ->
573         Bool}). Like in Haskell, polymorphic type variables always
574         start with a lowercase letter, while type constructors always start
575         with an uppercase letter.
576
577         \todo{How to define (new) type constructors?}
578
579         A special case of a type constructor is the \emph{function type
580         constructor}, \lam{->}. This is a type constructor taking two arguments
581         (using application below). The function type constructor is commonly
582         written inline, so we write \lam{a -> b} when we really mean \lam{-> a
583         b}, the function type constructor applied to \lam{a} and \lam{b}.
584
585         Polymorphic type variables can only be defined by a lambda
586         abstraction, see the forall type below.
587       \stopdesc
588
589       \startdesc{A type application}
590         \startlambda
591           Maybe Int
592         \stoplambda
593
594         This applies a some type to another type. This is particularly used to
595         apply type variables (type constructors) to their arguments.
596
597         As mentioned above, applications of some type constructors have
598         special notation. In particular, these are applications of the
599         \emph{function type constructor} and \emph{tuple type constructors}:
600         \startlambda
601           foo :: a -> b
602           foo' :: -> a b
603           bar :: (a, b, c)
604           bar' :: (,,) a b c
605         \stoplambda
606       \stopdesc
607
608       \startdesc{The forall type}
609         \startlambda
610           id :: \forall a. a -> a
611         \stoplambda
612         The forall type introduces polymorphism. It is the only way to
613         introduce new type variables, which are completely unconstrained (Any
614         possible type can be assigned to it). Constraints can be added later
615         using predicate types, see below.
616
617         A forall type is always (and only) introduced by a type lambda
618         expression. For example, the Core translation of the
619         id function is:
620         \startlambda
621           id = λa.λx.x
622         \stoplambda
623
624         Here, the type of the binder \lam{x} is \lam{a}, referring to the
625         binder in the topmost lambda.
626
627         When using a value with a forall type, the actual type
628         used must be applied first. For example haskell expression \hs{id
629         True} (the function \hs{id} appleid to the dataconstructor \hs{True})
630         translates to the following Core:
631
632         \startlambda
633         id @Bool True
634         \stoplambda
635
636         Here, id is first applied to the type to work with. Note that the type
637         then changes from \lam{id :: \forall a. a -> a} to \lam{id @Bool ::
638         Bool -> Bool}. Note that the type variable \lam{a} has been
639         substituted with the actual type.
640
641         In Haskell, forall types are usually not explicitly specified (The use
642         of a lowercase type variable implicitly introduces a forall type for
643         that variable). In fact, in standard Haskell there is no way to
644         explicitly specify forall types. Through a language extension, the
645         \hs{forall} keyword is available, but still optional for normal forall
646         types (it is needed for \emph{existentially quantified types}, which
647         Cλash does not support).
648       \stopdesc
649
650       \startdesc{Predicate type}
651         \startlambda
652           show :: \forall a. Show s ⇒ s → String
653         \stoplambda
654        
655         \todo{Sidenote: type classes?}
656
657         A predicate type introduces a constraint on a type variable introduced
658         by a forall type (or type lambda). In the example above, the type
659         variable \lam{a} can only contain types that are an \emph{instance} of
660         the \emph{type class} \lam{Show}. \refdef{type class}
661
662         There are other sorts of predicate types, used for the type families
663         extension, which we will not discuss here.
664
665         A predicate type is introduced by a lambda abstraction. Unlike with
666         the forall type, this is a value lambda abstraction, that must be
667         applied to a value. We call this value a \emph{dictionary}.
668
669         Without going into the implementation details, a dictionary can be
670         seen as a lookup table all the methods for a given (single) type class
671         instance. This means that all the dictionaries for the same type class
672         look the same (\eg contain methods with the same names). However,
673         dictionaries for different instances of the same class contain
674         different methods, of course.
675
676         A dictionary is introduced by \small{GHC} whenever it encounters an
677         instance declaration. This dictionary, as well as the binder
678         introduced by a lambda that introduces a dictionary, have the
679         predicate type as their type. These binders are usually named starting
680         with a \lam{\$}. Usually the name of the type concerned is not
681         reflected in the name of the dictionary, but the name of the type
682         class is. The Haskell expression \hs{show True} thus becomes:
683
684         \startlambda
685         show @Bool \$dShow True
686         \stoplambda
687       \stopdesc
688
689       Using this set of types, all types in basic Haskell can be represented.
690       
691       \todo{Overview of polymorphism with more examples (or move examples
692       here)}.
693         
694   \section[sec:prototype:statetype]{State annotations in Haskell}
695     As noted in \in{section}[sec:description:stateann], Cλash needs some
696     way to let the programmer explicitly specify which of a function's
697     arguments and which part of a function's result represent the
698     function's state.
699
700     Using the Haskell type systems, there are a few ways we can tackle this.
701
702     \subsection{Type synonyms}
703       Haskell provides type synonyms as a way to declare a new type that is
704       equal to an existing type (or rather, a new name for an existing type).
705       This allows both the original type and the synonym to be used
706       interchangedly in a Haskell program. This means no explicit conversion
707       is needed either. For example, a simple accumulator would become:
708
709       \starthaskell
710       type State s = s
711       acc :: Word -> State Word -> (State Word, Word)
712       acc i s = let sum = s + i in (sum, sum)
713       \stophaskell
714
715       This looks nice in Haskell, but turns out to be hard to implement. There
716       are no explicit conversion in Haskell, but not in Core either. This
717       means the type of a value might be show as \hs{AccState} in some places,
718       but \hs{Word} in others (and this can even change due to
719       transformations). Since every binder has an explicit type associated
720       with it, the type of every function type will be properly preserved and
721       could be used to track down the statefulness of each value by the
722       compiler. However, this makes the implementation a lot more complicated
723       than it currently is using \hs{newtypes}.
724
725     % Use \type instead of \hs here, since the latter breaks inside
726     % section headings.
727     \subsection{Type renaming (\type{newtype})}
728       Haskell also supports type renamings as a way to declare a new type that
729       has the same (runtime) representation as an existing type (but is in
730       fact a different type to the typechecker). With type renaming, an
731       explicit conversion between values of the two types is needed. The
732       accumulator would then become:
733
734       \starthaskell
735       newtype State s = State s
736       acc :: Word -> State Word -> (State Word, Word)
737       acc i (State s) = let sum = s + i in (State sum, sum)
738       \stophaskell
739
740       The \hs{newtype} line declares a new type \hs{State} that has one type
741       argument, \hs{s}. This type contains one \quote{constructor} \hs{State}
742       with a single argument of type \hs{s}. It is customary to name the
743       constructor the same as the type, which is allowed (since types can
744       never cause name collisions with values). The difference with the type
745       synonym example is in the explicit conversion between the \hs{State
746       Word} and \hs{Word} types by pattern matching and by using the explicit
747       the \hs{State constructor}.
748
749       This explicit conversion makes the \VHDL generation easier: Whenever we
750       remove (unpack) the \hs{State} type, this means we are accessing the
751       current state (\eg, accessing the register output). Whenever we are a
752       adding (packing) the \hs{State} type, we are producing a new value for
753       the state (\eg, providing the register input).
754
755       When dealing with nested states (a stateful function that calls stateful
756       functions, which might call stateful functions, etc.) the state type
757       could quickly grow complex because of all the \hs{State} type constructors
758       needed. For example, consider the following state type (this is just the
759       state type, not the entire function type):
760
761       \starttyping
762       State (State Bit, State (State Word, Bit), Word)
763       \stoptyping
764
765       We cannot leave all these \hs{State} type constructors out, since that
766       would change the type (unlike when using type synonyms). However, when
767       using type synonyms to hide away substates (see
768       \in{section}[sec:prototype:substatesynonyms] below), this
769       disadvantage should be limited.
770
771       \subsubsection{Different input and output types}
772         An alternative could be to use different types for input and output
773         state (\ie current and updated state). The accumulator example would
774         then become something like:
775
776         \starthaskell
777         newtype StateIn s = StateIn s
778         newtype StateOut s = StateOut s
779         acc :: Word -> StateIn Word -> (StateIn Word, Word)
780         acc i (StateIn s) = let sum = s + i in (StateIn sum, sum)
781         \stophaskell
782         
783         This could make the implementation easier and the hardware
784         descriptions less errorprone (you can no longer \quote{forget} to
785         unpack and repack a state variable and just return it directly, which
786         can be a problem in the current prototype). However, it also means we
787         need twice as many type synonyms to hide away substates, making this
788         approach a bit cumbersome. It also makes it harder to copmare input
789         and output state types, possible reducing the type safety of the
790         descriptions.
791
792     \subsection[sec:prototype:substatesynonyms]{Type synonyms for substates}
793       As noted above, when using nested (hierarchical) states, the state types
794       of the \quote{upper} functions (those that call other functions, which
795       call other functions, etc.) quickly becomes complicated. Also, when the
796       state type of one of the \quote{lower} functions changes, the state
797       types of all the upper functions changes as well. If the state type for
798       each function is explicitly and completely specified, this means that a
799       lot of code needs updating whenever a state type changes.
800
801       To prevent this, it is recommended (but not enforced) to use a type
802       synonym for the state type of every function. Every function calling
803       other functions will then use the state type synonym of the called
804       functions in its own type, requiring no code changes when the state type
805       of a called function changes. This approach is used in
806       \in{example}[ex:AvgState] below. The \hs{AccState} and \hs{AvgState}
807       are examples of such state type synonyms.
808
809     \subsection{Chosen approach}
810       To keep implementation simple, the current prototype uses the type
811       renaming approach, with a single type for both input and output
812       states. In the future, it might be worthwhile to revisit this
813       approach if more complicated flow analysis is implemented for
814       state variables. This analysis is needed to add proper error
815       checking anyway and might allow the use of type synonyms without
816       losing any expressivity.
817
818       \subsubsection{Example}
819         As an example of the used approach, there is a simple averaging circuit in
820         \in{example}[ex:AvgState]. This circuit lets the accumulation of the
821         inputs be done by a subcomponent, \hs{acc}, but keeps a count of value
822         accumulated in its own state.\footnote{Currently, the prototype
823         is not able to compile this example, since the builtin function
824         for division has not been added.}
825         
826         \startbuffer[AvgState]
827           -- The state type annotation
828           newtype State s = State s
829
830           -- The accumulator state type
831           type AccState = State Word
832           -- The accumulator
833           acc :: Word -> AccState -> (AccState, Word)
834           acc i (State s) = let sum = s + i in (State sum, sum)
835
836           -- The averaging circuit state type
837           type AvgState = State (AccState, Word)
838           -- The averaging circuit
839           avg :: Word -> AvgState -> (AvgState, Word)
840           avg i (State s) = (State s', o)
841             where
842               (accs, count) = s
843               -- Pass our input through the accumulator, which outputs a sum
844               (accs', sum) = acc i accs
845               -- Increment the count (which will be our new state)
846               count' = count + 1
847               -- Compute the average
848               o = sum / count'
849               s' = (accs', count')
850         \stopbuffer
851
852         \placeexample[here][ex:AvgState]{Simple stateful averaging circuit.}
853           %\startcombination[2*1]
854             {\typebufferhs{AvgState}}%{Haskell description using function applications.}
855           %  {\boxedgraphic{AvgState}}{The architecture described by the Haskell description.}
856           %\stopcombination
857         \todo{Picture}
858
859   \section{Implementing state}  
860     Now its clear how to put state annotations in the Haskell source,
861     there is the question of how to implement this state translation. As
862     we've seen in \in{section}[sec:prototype:design], the translation to
863     \VHDL happens as a simple, final step in the compilation process.
864     This step works on a core expression in normal form. The specifics
865     of normal form will be explained in
866     \in{chapter}[chap:normalization], but the examples given should be
867     easy to understand using the definitin of Core given above.
868
869         \startbuffer[AvgStateNormal]
870           acc = λi.λspacked.
871             let
872               -- Remove the State newtype
873               s = spacked ▶ Word
874               s' = s + i
875               o = s + i
876               -- Add the State newtype again
877               spacked' = s' ▶ State Word
878               res = (spacked', o)
879             in
880               res
881
882           avg = λi.λspacked.
883             let
884               s = spacked ▶ (AccState, Word)
885               accs = case s of (accs, _) -> accs
886               count = case s of (_, count) -> count
887               accres = acc i accs
888               accs' = case accres of (accs', _) -> accs'
889               sum = case accres of (_, sum) -> sum
890               count' = count + 1
891               o = sum / count'
892               s' = (accs', count')
893               spacked' = s' ▶ State (AccState, Word)
894               res = (spacked', o)
895             in
896               res
897         \stopbuffer
898
899         \placeexample[here][ex:AvgStateNormal]{Normalized version of \in{example}[ex:AvgState]}
900             {\typebufferlam{AvgStateNormal}}
901
902     \subsection[sec:prototype:statelimits]{State in normal form}
903       Before describing how to translate state from normal form to
904       \VHDL, we will first see how state handling looks in normal form.
905       What limitations are there on their use to guarantee that proper
906       \VHDL can be generated?
907
908       We will try to formulate a number of rules about what operations are
909       allowed with state variables. These rules apply to the normalized Core
910       representation, but will in practice apply to the original Haskell
911       hardware description as well. Ideally, these rules would become part
912       of the intended normal form definition \refdef{intended normal form
913       definition}, but this is not the case right now. This can cause some
914       problems, which are detailed in
915       \in{section}[sec:normalization:stateproblems].
916
917       In these rules we use the terms \emph{state variable} to refer to any
918       variable that has a \lam{State} type. A \emph{state-containing
919       variable} is any variable whose type contains a \lam{State} type,
920       but is not one itself (like \lam{(AccState, Word)} in the example,
921       which is a tuple type, but contains \lam{AccState}, which is again
922       equal to \lam{State Word}).
923
924       We also use a distinction between \emph{input} and \emph{output
925       (state) variables} and \emph{substate variables}, which will be
926       defined in the rules themselves.
927
928       \startdesc{State variables can appear as an argument.}
929         \startlambda
930           avg = λi.λspacked. ...
931         \stoplambda
932
933         Any lambda that binds a variable with a state type, creates a new
934         input state variable.
935       \stopdesc
936
937       \startdesc{Input state variables can be unpacked.}
938         \startlambda
939           s = spacked ▶ (AccState, Word)
940         \stoplambda
941
942         An input state variable may be unpacked using a cast operation. This
943         removes the \lam{State} type renaming and the result has no longer a
944         \lam{State} type.
945
946         If the result of this unpacking does not have a state type and does
947         not contain state variables, there are no limitations on its use.
948         Otherwise if it does not have a state type but does contain
949         substates, we refer to it as a \emph{state-containing input
950         variable} and the limitations below apply. If it has a state type
951         itself, we refer to it as an \emph{input substate variable} and the
952         below limitations apply as well.
953
954         It may seem strange to consider a variable that still has a state
955         type directly after unpacking, but consider the case where a
956         function does not have any state of its own, but does call a single
957         stateful function. This means it must have a state argument that
958         contains just a substate. The function signature of such a function
959         could look like:
960
961         \starthaskell
962           type FooState = State AccState
963         \stophaskell
964
965         Which is of course equivalent to \lam{State (State Word)}.
966       \stopdesc
967
968       \startdesc{Variables can be extracted from state-containing input variables.}
969         \startlambda
970           accs = case s of (accs, _) -> accs
971         \stoplambda
972
973         A state-containing input variable is typically a tuple containing
974         multiple elements (like the current function's state, substates or
975         more tuples containing substates). All of these can be extracted
976         from an input variable using an extractor case (or possibly
977         multiple, when the input variable is nested).
978
979         If the result has no state type and does not contain any state
980         variables either, there are no further limitations on its use. If
981         the result has no state type but does contain state variables we
982         refer to it as a \emph{state-containing input variable} and this
983         limitation keeps applying. If the variable has a state type itself,
984         we refer to it as an \emph{input substate variable} and below
985         limitations apply.
986
987       \startdesc{Input substate variables can be passed to functions.} 
988         \startlambda
989           accres = acc i accs
990           accs' = case accres of (accs', _) -> accs'
991         \stoplambda
992         
993         An input substate variable can (only) be passed to a function.
994         Additionally, every input substate variable must be used in exactly
995         \emph{one} application, no more and no less.
996
997         The function result should contain exactly one state variable, which
998         can be extracted using (multiple) case statements. The extracted
999         state variable is referred to the \emph{output substate}
1000
1001         The type of this output substate must be identical to the type of
1002         the input substate passed to the function.
1003       \stopdesc
1004
1005       \startdesc{Variables can be inserted into a state-containing output variable.}
1006         \startlambda
1007           s' = (accs', count')
1008         \stoplambda
1009         
1010         A function's output state is usually a tuple containing its own
1011         updated state variables and all output substates. This result is
1012         built up using any single-constructor algebraic datatype.
1013
1014         The result of these expressions is referred to as a
1015         \emph{state-containing output variable}, which are subject to these
1016         limitations.
1017       \stopdesc
1018
1019       \startdesc{State containing output variables can be packed.}
1020         \startlambda
1021           spacked' = s' ▶ State (AccState, Word)
1022         \stoplambda
1023
1024         As soon as all a functions own update state and output substate
1025         variables have been joined together, the resulting
1026         state-containing output variable can be packed into an output
1027         state variable. Packing is done by casting into a state type.
1028       \stopdesc
1029
1030       \startdesc{Output state variables can appear as (part of) a function result.}
1031         \startlambda
1032           avg = λi.λspacked.
1033             let
1034             \vdots
1035             res = (spacked', o)
1036           in
1037             res
1038         \stoplambda
1039         When the output state is packed, it can be returned as a part
1040         of the function result. Nothing else can be done with this
1041         value (or any value that contains it).
1042       \stopdesc
1043
1044       There is one final limitation that is hard to express in the above
1045       itemization. Whenever substates are extracted from the input state
1046       to be passed to functions, the corresponding output substates
1047       should be inserted into the output state in the same way. In other
1048       words, each pair of corresponding substates in the input and
1049       output states should be passed / returned from the same called
1050       function.
1051
1052       The prototype currently does not check much of the above
1053       conditions. This means that if the conditions are violated,
1054       sometimes a compile error is generated, but in other cases output
1055       can be generated that is not valid \VHDL or at the very least does
1056       not correspond to the input.
1057
1058     \subsection{Translating to \VHDL}
1059       As noted above, the basic approach when generating \VHDL for stateful
1060       functions is to generate a single register for every stateful function.
1061       We look around the normal form to find the let binding that removes the
1062       \lam{State} newtype (using a cast). We also find the let binding that
1063       adds a \lam{State} type. These are connected to the output and the input
1064       of the generated let binding respectively. This means that there can
1065       only be one let binding that adds and one that removes the \lam{State}
1066       type. It is easy to violate this constraint. This problem is detailed in
1067       \in{section}[sec:normalization:stateproblems].
1068
1069       This approach seems simple enough, but will this also work for more
1070       complex stateful functions involving substates?  Observe that any
1071       component of a function's state that is a substate, \ie passed on as
1072       the state of another function, should have no influence on the
1073       hardware generated for the calling function. Any state-specific
1074       \small{VHDL} for this component can be generated entirely within the
1075       called function.  So, we can completely ignore substates when
1076       generating \VHDL for a function.
1077       
1078       From this observation, we might think to remove the substates from a
1079       function's states alltogether, and leave only the state components
1080       which are actual states of the current function. While doing this
1081       would not remove any information needed to generate \small{VHDL} from
1082       the function, it would cause the function definition to become invalid
1083       (since we won't have any substate to pass to the functions anymore).
1084       We could solve the syntactic problems by passing \type{undefined} for
1085       state variables, but that would still break the code on the semantic
1086       level (\ie, the function would no longer be semantically equivalent to
1087       the original input).
1088
1089       To keep the function definition correct until the very end of the
1090       process, we will not deal with (sub)states until we get to the
1091       \small{VHDL} generation.  Then, we are translating from Core to
1092       \small{VHDL}, and we can simply ignore substates, effectively removing
1093       the substate components alltogether.
1094
1095       But, how will we know what exactly is a substate? Since any state
1096       argument or return value that represents state must be of the
1097       \type{State} type, we can look at the type of a value. However, we
1098       must be careful to ignore only \emph{substates}, and not a
1099       function's own state.
1100
1101       In \in{example}[ex:AvgStateNorm] above, we should generate a register
1102       connected with its output connected to \lam{s} and its input connected
1103       to \lam{s'}. However, \lam{s'} is build up from both \lam{accs'} and
1104       \lam{count'}, while only \lam{count'} should end up in the register.
1105       \lam{accs'} is a substate for the \lam{acc} function, for which a
1106       register will be created when generating \VHDL for the \lam{acc}
1107       function.
1108
1109       Fortunately, the \lam{accs'} variable (and any other substate) has a
1110       property that we can easily check: It has a \lam{State} type
1111       annotation. This means that whenever \VHDL is generated for a tuple
1112       (or other algebraic type), we can simply leave out all elements that
1113       have a \lam{State} type. This will leave just the parts of the state
1114       that do not have a \lam{State} type themselves, like \lam{count'},
1115       which is exactly a function's own state. This approach also means that
1116       the state part of the result is automatically excluded when generating
1117       the output port, which is also required.
1118
1119       We can formalize this translation a bit, using the following
1120       rules.
1121
1122       \startitemize
1123         \item A state unpack operation should not generate any \small{VHDL}.
1124         The binder to which the unpacked state is bound should still be
1125         declared, this signal will become the register and will hold the
1126         current state.
1127         \item A state pack operation should not generate any \small{VHDL}.
1128         The binder to which the packed state is bound should not be
1129         declared. The binder that is packed is the signal that will hold the
1130         new state.
1131         \item Any values of a State type should not be translated to
1132         \small{VHDL}. In particular, State elements should be removed from
1133         tuples (and other datatypes) and arguments with a state type should
1134         not generate ports.
1135         \item To make the state actually work, a simple \small{VHDL} proc
1136         should be generated. This proc updates the state at every
1137         clockcycle, by assigning the new state to the current state. This
1138         will be recognized by synthesis tools as a register specification.
1139       \stopitemize
1140
1141       When applying these rules to the description in
1142       \in{example}[ex:AvgStateNormal], we be left with the description
1143       in \in{example}[ex:AvgStateRemoved]. All the parts that don't
1144       generate any \VHDL directly are crossed out, leaving just the
1145       actual flow of values in the final hardware.
1146       
1147       \startlambda
1148         avg = iλ.--λspacked.--
1149           let 
1150             s = --spacked ▶ (AccState, Word)--
1151             --accs = case s of (accs, _) -> accs--
1152             count = case s of (--_,-- count) -> count
1153             accres = acc i --accs--
1154             --accs' = case accres of (accs', _) -> accs'--
1155             sum = case accres of (--_,-- sum) -> sum
1156             count' = count + 1
1157             o = sum / count'
1158             s' = (--accs',-- count')
1159             --spacked' = s' ▶ State (AccState, Word)--
1160             res = (--spacked',-- o)
1161           in
1162             res
1163       \stoplambda
1164               
1165       When we would really leave out the crossed out parts, we get a slightly
1166       weird program: There is a variable \lam{s} which has no value, and there
1167       is a variable \lam{s'} that is never used. Together, these two will form
1168       the state proc of the function. \lam{s} contains the "current" state,
1169       \lam{s'} is assigned the "next" state. So, at the end of each clock
1170       cycle, \lam{s'} should be assigned to \lam{s}.
1171
1172       As you can see, the definition of \lam{s'} is still present, since
1173       it does not have a state type. The \lam{accums'} substate has been
1174       removed, leaving us just with the state of \lam{avg} itself.
1175
1176       As an illustration of the result of this function,
1177       \in{example}[ex:AccStateVHDL] and \in{example}[ex:AvgStateVHDL] show the the \VHDL that is
1178       generated from the examples is this section.
1179
1180       \startbuffer[AvgStateVHDL]
1181         entity avgComponent_0 is
1182              port (\izAlE2\ : in \unsigned_31\;
1183                    \foozAo1zAo12\ : out \(,)unsigned_31\;
1184                    clock : in std_logic;
1185                    resetn : in std_logic);
1186         end entity avgComponent_0;
1187
1188
1189         architecture structural of avgComponent_0 is
1190              signal \szAlG2\ : \(,)unsigned_31\;
1191              signal \countzAlW2\ : \unsigned_31\;
1192              signal \dszAm62\ : \(,)unsigned_31\;
1193              signal \sumzAmk3\ : \unsigned_31\;
1194              signal \reszAnCzAnM2\ : \unsigned_31\;
1195              signal \foozAnZzAnZ2\ : \unsigned_31\;
1196              signal \reszAnfzAnj3\ : \unsigned_31\;
1197              signal \s'zAmC2\ : \(,)unsigned_31\;
1198         begin
1199              \countzAlW2\ <= \szAlG2\.A;
1200
1201              \comp_ins_dszAm62\ : entity accComponent_1
1202                                        port map (\izAob3\ => \izAlE2\,
1203                                                  \foozAoBzAoB2\ => \dszAm62\,
1204                                                  clock => clock,
1205                                                  resetn => resetn);
1206
1207              \sumzAmk3\ <= \dszAm62\.A;
1208
1209              \reszAnCzAnM2\ <= to_unsigned(1, 32);
1210
1211              \foozAnZzAnZ2\ <= \countzAlW2\ + \reszAnCzAnM2\;
1212
1213              \reszAnfzAnj3\ <= \sumzAmk3\ * \foozAnZzAnZ2\;
1214
1215              \s'zAmC2\.A <= \foozAnZzAnZ2\;
1216
1217              \foozAo1zAo12\.A <= \reszAnfzAnj3\;
1218
1219              state : process (clock, resetn)
1220              begin
1221                   if resetn = '0' then
1222                   elseif rising_edge(clock) then
1223                        \szAlG2\ <= \s'zAmC2\;
1224                   end if;
1225              end process state;
1226         end architecture structural;
1227       \stopbuffer
1228       \startbuffer[AccStateVHDL]
1229         entity accComponent_1 is
1230              port (\izAob3\ : in \unsigned_31\;
1231                    \foozAoBzAoB2\ : out \(,)unsigned_31\;
1232                    clock : in std_logic;
1233                    resetn : in std_logic);
1234         end entity accComponent_1;
1235
1236
1237         architecture structural of accComponent_1 is
1238              signal \szAod3\ : \unsigned_31\;
1239              signal \reszAonzAor3\ : \unsigned_31\;
1240         begin
1241              \reszAonzAor3\ <= \szAod3\ + \izAob3\;
1242              
1243              \foozAoBzAoB2\.A <= \reszAonzAor3\;
1244              
1245              state : process (clock, resetn)
1246              begin
1247                   if resetn = '0' then
1248                   elseif rising_edge(clock) then
1249                        \szAod3\ <= \reszAonzAor3\;
1250                   end if;
1251              end process state;
1252         end architecture structural;
1253       \stopbuffer 
1254     
1255       \placeexample[][ex:AccStateVHDL]{\VHDL generated for acc from \in{example}[ex:AvgState]}
1256           {\typebuffer[AccStateVHDL]}
1257       \placeexample[][ex:AvgStateVHDL]{\VHDL generated for avg from \in{example}[ex:AvgState]}
1258           {\typebuffer[AvgStateVHDL]}
1259 %    \subsection{Initial state}
1260 %      How to specify the initial state? Cannot be done inside a hardware
1261 %      function, since the initial state is its own state argument for the first
1262 %      call (unless you add an explicit, synchronous reset port).
1263 %
1264 %      External init state is natural for simulation.
1265 %
1266 %      External init state works for hardware generation as well.
1267 %
1268 %      Implementation issues: state splitting, linking input to output state,
1269 %      checking usage constraints on state variables.
1270 %
1271 %      \todo{Implementation issues: Separate compilation, simplified core.}
1272 %
1273 % vim: set sw=2 sts=2 expandtab: