Sat Apr 24 13:16:03 EDT 2010

Z -> loop

Last development: some algebraic manipulation of filters or state
space models.

*Main> printC $ compile [(z (z $ var "a")) + (z $ var "b")]
float fun() {
    float r0 = z(a);
    float r1 = z(r0);
    float r2 = z(b);
    float r3 = add(r1, r2);

The interesting part is that the `z' operator doesn't need special
treatment and will survive the SSA conversion.  After that it can be
given a special meaning as described in [1].

Todo: convert a MIMO function with unit delays into a C loop.

-- Basic idea: 
--  * wrap the code in a "for (i=0; i<n; i++) { ... }" loop
--  * convert input/output access to indexed array accesses
--  * convert delay ops to struct refs

It might be simplest to again not bother with C syntax and use
reserved names instead.

#define aref(a,i) a[a]
#define sref(a,field) a.field

- The dereference aref conversion is trivial (if a variable name is a
  signal, replace it with a ref, otherwise leave it alone). 

- For outputs, the "return" statement in C code rendering needs to be
  replaced with a proper assignment operation.

- State updates work like this.  For each

             r1 <- (z r0)

  we generate

             r1 = sref(state, r0);
             sref(state,r0) = r0;

  A `fold' over the code can then collect all the state variables and
  construct declaration code for the state struct.

Main problem: how to incorporate proper assignments in the C code

data Let      = Let      String String [String]  deriving Show
data Function = Function [String] [Let] [String] 

The "return" statement needs to be replaced with assignments.  The
rest seems trivial.  (Is it me, or is this really the only place where
the functional picture breaks down a bit: the allocation of storage
space for function results?).

[1] entry://20100220-145657