Hi all,

I've released a C++ library for runtime code generation on x86/x64
processors which I dubbed Erasm++ (for Embedded Runtime Assembler in
C++). The code looks like this:

p += add    (p, al , byte_ptr );
p += cmps    (p, byte_ptr.ds , byte_ptr.es);
p += movaps  (p, xmm0 , xmmword_ptr[ _5* rax ]);

Although similar runtime code generator libraries already exist,
I believe my library has the following strengths:

  • It is easier to use because the syntax is more natural.

  • It is safer to use because the syntactically invalid expressions
    cause compilation errors, rather than runtime errors or exceptions.

  • It is faster because most of the encoding (and the error
    checking) is done at compile-time, rather than at runtime. 

It's fast -- indeed, with recent compilers, each generator function
is translated into a series of MOV instructions that contain no
external references (except possibly the stack and the argument).

For example, the following code

extern "C" int  code_test(code_ptr p,int32_t n)
    return adc(p ,cl,byte_ptr );

is compiled to:

sub esp, 32
mov eax, DWORD PTR
mov BYTE PTR , -88
mov edx, DWORD PTR
mov DWORD PTR , edx
mov BYTE PTR , -116
mov BYTE PTR , 103
mov BYTE PTR , 18
mov edx, DWORD PTR
mov DWORD PTR , edx
mov edx, DWORD PTR
mov WORD PTR , dx
mov eax, 8
add esp, 32

On the other hand, the downside is that the compilation is noticeably
slower than other systems. Also, compilation errors can lead to
somewhat cryptic error messages (although I have made some effort to
mitigate this.)

The distribution also includes a very fast instruction decoder library
called GenericDSM, and a Haskell-like lazy metaprogramming library
called MetaPrelude which greatly helps implementing EDSLs in C++.

If you are interested, here are my project pages at freecode and GitHub:

I hope you enjoy it, and if you've found bugs in the code,or in my
English, have a feature request, or whatever, please contact me at

Also, I would like to ask a few questions to assembly programmers here:

  • How do you optimize the above code? What would be the smallest,
    or the fastest, assembly code that does the same thing and has the same
    properties (no branches,no external references)? 

  • I feel the increased compilation time due to heavy
    metaprogramming can be justified if the runtime performance is
    good. Would you agree in this particular case? (I will consider
    implementing a debug version for fast compilation if users really
    need it.) 

  • I'm not a western guy, but a quick googling suggested to me that
    the library name might sound a bit pompous. What do you think?  Should
    I rename it? 

Thank you very much!
Posted on 2012-02-01 06:12:02 by mnish