Re: On the fly assembly language generation.
From: Basile Starynkevitch [news] (basile-news_at_starynkevitch.net)
Date: Wed, 19 Oct 2005 02:56:52 +0000 (UTC)
On 2005-10-18, email@example.com <firstname.lastname@example.org> wrote:
> Let's say I was a total nutcase and I wanted to generate some assembly
> code dynamically in a Linux Application. [...]
In addition to others replies about using mmap:
you probably want to generate machine code dynamically, not assembly
code. Assembly code is textual file, you can easily generate it with
More seriously, for generating machine code dynamically, you could use
GNU lightning http://www.gnu.org/software/lightning/lightning.html
or LibJit http://www.southern-storm.com.au/libjit.html
Lightning works on x86, PowerPC32, Sparc (but not on AMD64=x86_64)
LibJit works really on x86, ARM (and generate "bytecode" on other
plateforms). Using lightning is quite easy (but the generated code is
usually not very optimised, less that what gcc -O1 gives on the
equivalent C function).
Sadly, I don't know yet of any such machine code generating library
working on AMD64 (and taking advantage of its registers).
Also, you could generate at runtime a C file foo.c (by printing some
appropriate abstract syntax tree generated within your program),
compile it (running from your program gcc -shared -fPIC -O foo.c -o
foo.so) and dlopen it. This works quite well, even if the overhead of
generating C and compiling it is significant. And as the manydl.c
program on my home pages demonstrate, you can do a lot of dlopen
together in a single process (I tested up to 30 000 dlopen)...
If you want to generate many short-lived small functions (which are
not very optimised), I'll suggest using Lightning or maybe LibJit; if
you want to generate bigger longer-living functions, I tend to suggest
generating C code and dynamically compiling it and dlopen-ing it.
An alternative would be to use staged programming languages like
CommonLisp (SBCL) or MetaOcaml (www.metaocaml.org)
If some reader knows about an opensource machine code generator
library working on AMD64, please let us know!
-- Basile STARYNKEVITCH http://starynkevitch.net/Basile/ email: basile(at)starynkevitch(dot)net 8, rue de la Fa´encerie, 92340 Bourg La Reine, France