This section of the archives stores flipcode's complete Developer Toolbox collection, featuring a variety of mini-articles and source code contributions from our readers.

 

  Run-Time Intrinsics
  Submitted by



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:
Assembler x86;
int *array = new int[100];

x86.add(eax, dword ptr[array+ecx+ebx*4+16]); x86.push((int)array); x86.call((int)printf); x86.ret();



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 c0d1f1ed@users.sourceforge.net. 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. Regards,
Nicolas "Nick" Capens


Download Associated File: sw-runtimei.zip (367,461 bytes)

The zip file viewer built into the Developer Toolbox made use of the zlib library, as well as the zlibdll source additions.

 

Copyright 1999-2008 (C) FLIPCODE.COM and/or the original content author(s). All rights reserved.
Please read our Terms, Conditions, and Privacy information.