Skip to content
Kensuke Sawada edited this page Jan 30, 2016 · 5 revisions

Compiling To ANSI-C

Top-level Elements

Top-level elements in Emfrp are simply compiled into C's elements as follows:

basic Assignment

Note that Emfrp's functions and types are polymorphic but C's are monomorphic. The compiler converts an Emfrp's polymorphic function/type into C's multiple monomorphic functions/structs as it is instantiated.

Expressions

In expression level, there are no big gaps between Emfrp and C. It is, however, desirable for a great performance to implement some additional optimizing processes on current(v0.1.2) Emfrp's compiler.

FRP Runtime-process

The FRP runtime-process is simple too. In Emfrp, dependencies of time-varying values are expressed by a directed-asyclic-graph(DAG). Nodes can be statically sorted by order of evaluation. For example, in case of a directed-graph illustrated by following image, nodes can be sorted as a -> c -> e -> b -> f -> d.

sample graph

If nodes are sorted, all runtime-process should do is evaluate nodes in that order while using other evaluated values.

Allocation For Data-types

Instances of Emfrp's data-types' entities are not allocated on either stack-area or heap-area. They are allocated on static-area. This is possible because Emfrp doesn't allow recursive data-types and recursive function-calls. In other words, an arbitrary Emfrp's module has limits of allocations for all data-types.

Let's show that by an example:

module Example
in
  inputA : Int,
  inputB : Int
out
  z
use
  Std

func padd(p1, p2) = (p1.fst + p2.fst, p1.snd + p2.snd)

node x : (Int, Int) = (inputA, inputB)
node y : (Int, Int) = x `padd` (1, 1)
node z : (Int, Int) = y `padd` y

In case of the module, following facts are able to say:

  • Node x instantiates one Tuple2(Int, Int) and none of other data-types.
  • Node y instantiates two Tuple2(Int, Int) and none of other data-types.
  • Node z instantiates one Tuple2(Int, Int) and none of other data-types.

And, following facts are able to say:

  • When calculating node x, none of data-types need to be held.
  • When calculating node y, only the calculated value of node x needs to be held.
  • When calculating node z, only the calculated value of node y needs to be held.

From the above, it is able to say that only three (not four!) allocations of data-type Tuple2(Int, Int) is required to calculate whole the module.

In this way, an arbitrary Emfrp's module has limits of allocations for all data-types. Therefor, Emfrp compiler can statically allocate data-types' memory on static-area by its limits.

Clone this wiki locally