This is the mail archive of the gcc@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]

Re: Function Argument Question


On Tue, Dec 12, 2000 at 04:15:13PM -0600, Davy Durham wrote:
> Hi, I'm developing a scripting language in which I want to call external
> C functions...
> 
> The script is invoked by a C program..... but before it is, it sets a
> table of addresses that are pointers to the C functions I want to
> call...
> 
> However, what I need to know how to do is to say at what address the
> arguments begin...
> 
> If I do something like:
>     void func(int a,int b,int c)
>     {
>         printf("a:0x%x b:0x%x c:0x%x\n",&a,&b,&c);
>     }
> 
> It prints that a, b, and c are in contiguous memory... So I just need to
> say where this memory starts...

Not necessarily.  The compiler on many machines receives the first n arguments
in registers and not on the stack.  Some machines reserve space on the stack to
store these arguments, and some do not, in which case it stores the argument at
some location in the stack frame.  Also for char/short/float arguments if a
prototype was not in scope the compiler receives the argument as the widened
type and then converts it to the narrowed type, and stores it some place on the
stack.

So your only portable alternatives is to:

   1)	Do a giant switch statement within the scripting language, that covers
	every single combination of arguments passed, and call the function
	that way.

   2)	Encode the arguments into a giant text string and pass a single char *,
	decoding in your function.

Other solutions that come to mind that involve some amount of non-portable
code:

   1)	Build a module that knows the argument layout as a shared library and
	have the scripting language ldopen it to do the transfer.  This assumes
	your scripting language supports shared libraries and provides a way
	for C code to access its arguments.

   2)	Build a structure containing all of the data and pass it's address to
	the function.  This assumes you know the structure layout of the
	machine (which is different for different machines).

   3)	Run the C code as a separate process, using pipes or sockets, etc. to
	communicate.  This of course assumes you have pipes or sockets, and
	multiple processes.

In general, the marshalling and demarshalling of arguments tends to be the
Achilles heel of any mixed language (or RPC) system.

-- 
Michael Meissner, Red Hat, Inc.
PMB 198, 174 Littleton Road #3, Westford, Massachusetts 01886, USA
Work:	  meissner@redhat.com		phone: +1 978-486-9304
Non-work: meissner@spectacle-pond.org	fax:   +1 978-692-4482

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