Generating machine code directly to memory


Is it possible to produce machine code directly to memory buffer so that it could be called immediately? What I would like to do is something like this:

Scripting language (Antlr) ->
(Optimization) ->
Raw Machine code ->
(Transport media) ->

The idea is that I could compile my scripts directly to asm without any extra steps and in run-time. Then those asm pieces could be loaded back later without need to use Antrl/LLVM at all.

I've already been able to turn script into asm files using antlr/llvm but there's an extra step to compile the asm... Is it possible to emit code directly to memory and then execute it somehow?

LLVM BC with JIT is just fine, but I really would like to release my soft without LLVM at all.


- Sampsa

It seems that this is off topic for this list then... no?



The problem is that statically compiled programs (other than code that runs directly on the hardware, like firmware code) are not just a blob on binary instructions. They are wrapped in complicated linker formats (ELF, Mach-O, PE, etc.) containing metadata, dynamic linker instructions, and lots of other stuff. Hence the problem is a lot more complicated than just dumping out, say, the hex output of the JIT.


Yes, just compile to a .s file normally, then assemble that, then you can use ld and objcopy (binutils) to convert to something approaching raw machine code.