views:

615

answers:

4

Has anyone come across a framework or library for Delphi to simplify the generation of x86 code? I am not looking for an assembler, but rather a framework that abstracts the code generation process above the low level bits and bytes. Ideally I would like to build on top of an existing library or framework rather than hardcode the logic on a case by case basis.

The initial usage will be to generate small code stubs at runtime similar to the way Delphi dispatches SOAP requests. If I cannot find something I will likely roll my own, but I would hate to reinvent the wheel. Something in "C" might me interesting provided the license will permit translation and use in commercial and open source projects.

Update:

Here is some more context: What I am working toward is runtime implementation of interfaces and/or classes as part of a persistence framework. Something sort of like Java annotation driven persistence (JPA/EJB3) except with a distinctly Delphi flavor. The invocation target is a modular/extensible framework which will implement a generalized persistence model. I need to dispatch and hook method calls based on RTTI and an annotation/attribute model (something similar to InstantObjects metadata) in a very dynamic and fluid manner.

Thanks, David

+3  A: 

The more I have thought about your question. I am not sure if all you trying to just do Dynamic Method Invocation. Even though your asking about generating x86 code. There are several techiniques that do this.

If you know the signature of the method in question you can do it easily by using a TMethod and setting the method address and data.

procedure TForm8.Button1Click(Sender: TObject);
begin
  Showmessage('Hello1');
end;

procedure TForm8.Button2Click(Sender: TObject);
var
 M : TMethod;
begin
  M.Code := MethodAddress('Button1Click');
  M.Data := Self;
  TNotifyEvent(M)(self);
end;

If you don't know the method signature you can write the class with {$METHODINFO ON} Then use the functionality in ObjAuto.pas to invoke the method.

I have an example in my RTTI Presentation code from DelphiLive on how to do that.

Robert Love
+1  A: 

According to features of PaxCompiler, you can create stand alone executable files.

ErvinS
I have an older version of PaxCompiler before that feature was added. That native code generation feature sounds interesting and is on my list of things to play with. Probably not a great fit from an open source licensing perspective.
David Taylor
+1  A: 

Very spectulative answer: Something like LLVM? I am not sure if it can be used from delphi or not, but you should be able to create dll's wth it.

SeanX
Yes, something like this, but perhaps more Delphi specific and it does not need to be as capable. Ideally something that understands supported Delphi calling conventions (perhaps integrating with RTTI) and can do some basic code generation (assembly knowledge required of course).
David Taylor
A: 

Logically you would simply generate delphi code, compile to a DLL/BPL by cmdline compiler and then dyn load that one?

Unfortunately Delphi Explorer doesn't come with the cmdline compiler though. And your main binary would also have to be in Delphi Explorer (or at least in D2006 if that is binary compatible enough)

Any mix of Delphi versions (or Free Pascal) will probably not work on the package or HLL level, only at basic procedural DLL level.

Marco van de Voort
Hi Marco - I really want something more dynamic. I can code in inline assembler in Delphi, but cannot readily guess all of the permutations that will be needed up front.
David Taylor