The compiler uses A-Normal Form (ANF). Is under 1K lines of Lua.
github.com/zwizwa/uc_tool
Conversation
Still just a toy. Bugs likely. Current "test" is
github.com/zwizwa/uc_tool
1
Anything that crosses an SM_READ macro (the blocking call) is stored in a C struct. The rest goes on the C stack. This is ANF with constants bound to variables as well, to make implementation simpler. C compiler can take care of optimizing those away.
1
The yield/block points are (later) implemented on top of this protothread code. See e.g. SM_WAIT
1
Started adding support for CSP rendezvous (csp.h in uc_tools, written in hard-to-use C macros).
Added support for zero-copy mode. Eventually this needs to be fast (for the app: 137k int/sec on 72MHz CM3). Might need to specialize the scheduler further.
1
BTW doing this in Lua isn't all that bad. Of course refactoring is hard, but doable at this 1kloc complexity level.
However manual let-insertion is annoying. Older Haskell code used a state-continuation monad for that, which is really convenient. Almost magic.
2
End of winter break. Experiment succeeded. Anything more complex needs better abstraction. Feeling a Racket itch coming up...
1
C output doesn't look too bad. Compiles to invocation of csp.h macros for event setup (CSP_EVT_BUF), blocking select call (CSP_SEL) and case statement for dispatch.
1
From select form in
github.com/zwizwa/uc_tool
2
Going to have to specialize the CSP scheduler as well to make this fast. A bit more than a weekend project, but hey I'm learning things.
Replying to
Of course, while procrastinating, had to do the Scheme interpreter in Lua thing as well:
github.com/zwizwa/uc_tool
1
1
And preliminary compiler from Scheme subset to Lua github.com/zwizwa/uc_tool
We're using luajit so this might even be useful. Direct mapping, no tail calls.
Replying to
The big lesson is: turn more things into pure functions. It makes partial evaluation trivial. All the work is then moved to purifying library code (in this case the scheduler). E.g. wrap an if n==1 to expose a pure trivial base case to the compiler (while n>1 might be non-trival)
Replying to
There is a lot of room to optimize things. One case that happens a lot is a task that only blocks on a single channel. This is just a co-routine and doesn't need to interact with any scheduler data structures.
1
Co-routines in my implementation would be just state pointer swap + (computed) goto. Not even a function call. Seems doable without turning it into a big production.
1
1
Show replies
