Staapl

TL;DR

Staapl is a "macro assembler on steroids" for PIC18 microcontrollers. It can be used for writing firmware optimized for size. It goes where C can't go, and where assembler would be too complicated. It is based on Scheme (code generator) and Forth (target language / machine model).

About

Staapl uses custom code generators (macros, partial evaluation, metaprogramming, staging, ...) to generate code that is severely resource-constrained.

Staapl is an extension of the Racket programming language, a Scheme dialect with strong support for embedded domain specific languages (EDSL) written in terms of Scheme macros. A Staapl target language is a collection of target code generator macros.

The language for the Microchip PIC18 is a language based on on Forth. It has an optimizing peephole compiler, and a tethering mechanism with interactive console for running-target code updates through the PicKit2 programmer.

Unlike traditional Forth which is highly reflective, Staapl is built using macros with phase separation, which breaks the circular reflection into a directed language tower, creating a static semantics (the meaning of the language doesn't change inside one module, in contrast with original Forth's approach). Most of Staapl is organized around a convenient abstract syntax based on a typical concatenative combinator language like Joy. This syntax can then be used as a base for different interpretations or as a target for concrete syntax frontends.

(With the risk of being too vague..) While the PIC18 language is a stack language, the basic code generator structure could support any kind of "algebraic" language (point-free languages without variable bindings). Note that "point-free" only refers to the basic compoisition mechanism. The core substrate of Staapl is still Scheme, so primitive macros can have local variables if that simplifies programming. This is used effectively in the Staapl Forth for PIC18.

The basic idea is that point-free languages are easily manipulated on the meta-level which makes them an ideal code generation target. 1. Such languages have no variables that complicate code generation due to scope issues (hygiene). 2. Thanks to the associativity of the operations of concatenation (syntactically) and function composition (semantically), computations are trivially partitioned in compile time and run time. 3. Reliance on combinators for program structure (as opposed to explicit recursion and loops) allows for the derivation of program transformation laws that can be used for program derivation or compilation to efficient implementations.

Status

The phase I goal - a usable system for PIC18, be it with some rough edges - is reached, and the corresponding code is put in incremental improvement mode.

This base will serve as the basis for phase II: further experiments with point-free domain specific languages for DSP/control, static verification, and more elaborate compilation techniques to implement them.

At this time, Staapl is only practical for the PIC18 architecture. While writing other ports should be straightforward, it is a lot of work. I have some code for the 12 and 14 bit PIC cores, and I've been looking into porting staapl to PIC24/30/33 (dsPIC). The main hurdle is writing a peephole optimizer for the new architecture.

Documentation

Documentation is loosely split into two parts: one that documents the whole system from a Scheme p.o.v. and one that exposes only the low-level Forth language view. ( NOTE: Some of this is a bit outdated. If you're interested and having trouble getting things running, I'd be glad to help out in return for some feedback. Send me an email. )

Since this is a developer-oriented project with a lot of effort put into clean organization, I suggest looking at the source code to pierce through the top-layer of workflow glue. Staapl is structured as a library with multiple entry points, not a monolithic application. I encourage you to build your own workflow on top of it, and integrate it with other tools. (And share that code!).

Hire Me

Staapl is best fit for producing turnkey burn-and-forget binary firmware blobs, with no constraints on tools for team-integration. The basic idea is that I can be cheap as long as you don't need to modify the code yourself. Not that you can't or that I'm reluctant to provide source code and build tools, but there's a steep learning curve involved. ( Powerful tools do take some investment. )

Projects

The PIC18 Forth language has been used in:

Download

Staapl is part of PLaneT. To install the command line Forth compiler frontend, install PLT Scheme 4.1 or newer (only MzScheme is required) and type this with administrator privileges:

  mzscheme -p zwizwa/staapl/install
This will install the staaplc wrapper application alongside mzscheme. All Scheme modules are accessible using standard PLaneT paths.

For the latest changes track the darcs archive (hashed darcs-2 format). If you need a snapshot that doesn't depend on darcs, just ask. The Staapl source code, including that of its subprojects, is released to the public under the terms of the GPL v2. Staapl is written by Tom Schouten.

Sun May 20 10:45:32 EDT 2012