This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

best way to construct function calls at runtime?


Hello,

What is the best way to construct function calls at runtime?

I'm writing an interpreted language and want to access system/dynamic
libraries.  Dlsym gives me a function pointer and I have runtime
declarations identifying the function arguments.  But what is the right way
to invoke the call?

The ideal seems to be some kind of gcc library which will help me call
functions based on runtime interface declarations - perhaps I'm overly
hopeful but does such a beast exist?

Below are the things I've experimented with, all work on x86 but have
limitations and are unlikely to be very cross platform friendly.


cheers, Oliver.


INLINE ASSEMBLER
====================
Using inline assembly instructions (PUSH, CALL, MOV, STL) to put args on the
stack, call the function and retrieve scalar or floating-point result.  

CON: Works great on x86 but for other processors it looks a lot more scary.
CON: More complicated to handle functions returning struct/union types


ALL POSSIBLE COMBINATIONS
================================
Since args are boundary aligned I could do what most other interpreters I've
found do, do a switch for argcount and make a generic call for every
possibility.  Something like this...

  void** buf[siz];  //populated with vals (note that a double will fill two
words)
  switch (siz) {
    case 0: res=fn(); break;
    case 0: res=fn(buf[0]); break;
    case 0: res=fn(buf[0],buf[1]); break;
    case 0: res=fn(buf[0],buf[1],buf[2]); break;
    // et al.
  }

CON: have to repeat for different result types (scalar vs double vs
struct/union)
CON: seems like there should be a better way


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]