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

2
  • Delphi does not use code generation to dispatch SOAP Requests. It done through RTTI information found with {$METHODINFO} and ObjAuto.pas. I have example apps if your intrested on how that works. Commented May 29, 2009 at 4:50
  • Technically you are correct, but the invocation logic does in fact setup stack frames and parameters necessary to dispatch calls based on RTTI. This requires some low level work in assembler. I want to abstract this process and also be able to generate short code sequences for things like thunks which are a little bit more complex. Commented May 29, 2009 at 5:11

5 Answers 5

5

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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

1 Comment

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.
2

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.

1 Comment

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).
0

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.

1 Comment

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.
0

I just found an interesting framework that does much of what I was looking for when I originally posted the question. A little late for my purposes, but thought someone else might find this useful:

DAsmJit a Delphi port of the asmjit project

1 Comment

Too bad its an old port. Looking for newer port.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.