On Wed, Dec 19, 2012 at 8:08 AM, Tom Hawkins <tomahawkins@gmail.com> wrote:
A few folks have asked me about building EDSLs in Haskell for assembly programming, so I've posted an example of the approach we have had success using at BAE Systems.

It's a bit rough, so if anyone's motivated to polish it up, by all means.

  https://github.com/tomahawkins/asm-dsl-example/

Thanks. It was nice to read. I was reminded of "The Monad Reader Issue 6" as it explains how to "tie the knot" when creating labels: http://www.haskell.org/wikiupload/1/14/TMR-Issue6.pdf

The Russell uses an assembly language DSL as a motivating example and explains how either Haskell98 or mdo can be used to tie the knot, with mdo giving a nicer implementation. The result allows labels to be defined where they are used instead of requiring the user to instantiate them and then associate them with a place in the program. This provides a nice abstraction for building your assembler macros.

For example, in your code we see:
\begin{code}
testProgram :: Asm ()
testProgram = do
  begin <- label
  loop <- label
  begin -: do
    i1 10 20
    i2
    i3 5
  loop -: do
    i4
    goto loop
\end{code}

The corresponding code using the TMR trick might look like this (I've removed the nested indentation):
\begin{code}
testProgram :: Asm ()
testProgram = mdo
begin <- label
  i1 10 20
  i2
  i3 5
loop <- label
  i4
  goto loop
\end{code}

Other examples demonstrate that labels can be referenced before the line that creates them due to the way mdo works.

Jason