public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* best way to construct function calls at runtime?
@ 2004-08-12  6:03 George Oliver
  2004-08-12 12:06 ` Falk Hueffner
  0 siblings, 1 reply; 2+ messages in thread
From: George Oliver @ 2004-08-12  6:03 UTC (permalink / raw)
  To: 'gcc-help@gcc.gnu.org'

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

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: best way to construct function calls at runtime?
  2004-08-12  6:03 best way to construct function calls at runtime? George Oliver
@ 2004-08-12 12:06 ` Falk Hueffner
  0 siblings, 0 replies; 2+ messages in thread
From: Falk Hueffner @ 2004-08-12 12:06 UTC (permalink / raw)
  To: George Oliver; +Cc: 'gcc-help@gcc.gnu.org'

George Oliver <oliver.george@dhhs.tas.gov.au> writes:

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

You can use libffi, which is part of gcc's source. It is not ported to
all gcc platforms, but to most important ones.

-- 
	Falk

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2004-08-12  6:38 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-08-12  6:03 best way to construct function calls at runtime? George Oliver
2004-08-12 12:06 ` Falk Hueffner

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).