When developing a scripting engine, an emulator or a JIT-compiler, one of the hardest parts is the efficient code generation. Interpreting bytecodes is slow, and manually writing machine code is a nightmare...
This problem can be solved elegantly with a new feature in the SoftWire run-time assembler library called run-time intrinsics. Basically it is a list of member functions for the Assembler class with the same names as x86 assembly instructions, which generate the corresponding machine code at run-time. Instead of first generating a file with the assembly code which then needs to be parsed, you can take a shortcut and generate machine code instantly.
An example of the syntax is:
int *array = new int;
x86.add(eax, dword ptr[array+ecx+ebx*4+16]);
As you can see, it tries to imitate the assembly syntax as closely as possible. Only in a few situations an explicit cast is needed, which I might solve in the future. Most part of the syntax is checked at compile-time, to avoid surprises at run-time. An exception is the scale factor in a memory reference. A simple working example can be found in the attached source code. More details about syntax and usage can be found in Readme.html.
What are the benefits of run-time intrinsics? A common method to generate code at run-time is to 'stitch' together a couple of functions which have no stack management or return instructions, so-called 'naked' functions. This can be very useful, but it limits you to a static set of code snippets and it can be a lot of work. You often need dynamic register allocation, or even simply other constants or pointers. All this and much more is a lot easier with run-time intrinsics. Don't forget that this is still written in C++, so things like run-time conditional compilation become trivial.
This code is still in a beta stage. Although it looks promising, I'm
sure it contains lots of problems I'm not aware of. If you find any
bugs, do not hesitate to report them to the tracker at
SourceForge.net. Feature requests can also be made via that page. If
you're shy or you just want to discuss something personally, send an
e-mail to email@example.com. Constructive criticism here at flipCode would be much appreciated. At the moment the code is not that neat, but I hope I can improve that once it has good stability.
The latest version will be available at the SoftWire home page: softwire.sourceforge.net, or via the CVS at the project page.
Nicolas "Nick" Capens
The zip file viewer built into the Developer Toolbox made use
of the zlib library, as well as the zlibdll source additions.