9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
* Re: [9fans] calling function with (variable number of) parameters
@ 2001-06-21 14:33 rog
  2001-06-21 15:16 ` Dan Cross
  2001-06-21 15:17 ` Latchesar Ionkov
  0 siblings, 2 replies; 12+ messages in thread
From: rog @ 2001-06-21 14:33 UTC (permalink / raw)
  To: 9fans

> So save space for n + 1 parameters on the stack; the last won't be used
> by your called function, and you could store the number of parameters in
> it.  When you return, pop the last item off the stack, and use that to
> recalculate the frame pointer.

no actually, parameters are pushed on last first, so the last one
you've pushed (the one you've got access after the return) is the first
parameter to the function (which doesn't expect it).

i think that probably the only way of getting around the problem is to
change the prototype of the called function so that the first parameter
signifies the number of parameters.

the only other alternative is if the function is guaranteed to be
non-reentrant (fat chance) in which case you could store the number of
params in a static variable.

of course, that then makes it incompatible with the original...

  cheers,
    rog.



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

* Re: [9fans] calling function with (variable number of) parameters
  2001-06-21 14:33 [9fans] calling function with (variable number of) parameters rog
@ 2001-06-21 15:16 ` Dan Cross
  2001-06-21 15:17 ` Latchesar Ionkov
  1 sibling, 0 replies; 12+ messages in thread
From: Dan Cross @ 2001-06-21 15:16 UTC (permalink / raw)
  To: 9fans

In article <20010621142423.D9AFF199EB@mail.cse.psu.edu> you write:
>no actually, parameters are pushed on last first, so the last one
>you've pushed (the one you've got access after the return) is the first
>parameter to the function (which doesn't expect it).

Oh, yeah; dang, I was thinking backwards.

	- Dan C.



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

* Re: [9fans] calling function with (variable number of) parameters
  2001-06-21 14:33 [9fans] calling function with (variable number of) parameters rog
  2001-06-21 15:16 ` Dan Cross
@ 2001-06-21 15:17 ` Latchesar Ionkov
  2001-06-29 22:11   ` Boyd Roberts
  1 sibling, 1 reply; 12+ messages in thread
From: Latchesar Ionkov @ 2001-06-21 15:17 UTC (permalink / raw)
  To: 9fans

Both suggestions are not really alternatives in that case. I am trying to
implement already defined interface, I don't have control on the way the
called functions are declared and they are definitely reentrant. :((

I can, though, hope that nobody in his mind will wrote a function with more
that 15 arguments. Then reserve space for 15 integers in the stack, put as
many argument as needed, call the function and remove 15 integers from the
stack. Of course I'll check and signall error if somebody is crazy enough to
try to use the code with more than 15 arguments.

Thanks again,

	Lucho

On Thu, Jun 21, 2001 at 03:33:15PM +0100, rog@vitanuova.com said:
> > So save space for n + 1 parameters on the stack; the last won't be used
> > by your called function, and you could store the number of parameters in
> > it.  When you return, pop the last item off the stack, and use that to
> > recalculate the frame pointer.
> 
> no actually, parameters are pushed on last first, so the last one
> you've pushed (the one you've got access after the return) is the first
> parameter to the function (which doesn't expect it).
> 
> i think that probably the only way of getting around the problem is to
> change the prototype of the called function so that the first parameter
> signifies the number of parameters.
> 
> the only other alternative is if the function is guaranteed to be
> non-reentrant (fat chance) in which case you could store the number of
> params in a static variable.
> 
> of course, that then makes it incompatible with the original...
> 
>   cheers,
>     rog.


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

* Re: [9fans] calling function with (variable number of) parameters
  2001-06-21 15:17 ` Latchesar Ionkov
@ 2001-06-29 22:11   ` Boyd Roberts
  0 siblings, 0 replies; 12+ messages in thread
From: Boyd Roberts @ 2001-06-29 22:11 UTC (permalink / raw)
  To: 9fans

strangely enough, yes are singing:

    think it over

good advice i'd say.




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

* Re: [9fans] calling function with (variable number of) parameters
  2001-06-20 19:54 ` Latchesar Ionkov
  2001-06-21 12:39   ` Douglas A. Gwyn
@ 2001-06-21 14:13   ` Dan Cross
  1 sibling, 0 replies; 12+ messages in thread
From: Dan Cross @ 2001-06-21 14:13 UTC (permalink / raw)
  To: 9fans

In article <20010620155421.A1669@gmx.net> you write:
>I've already tried to write it in assembler. The problem is that after the
>function returns, I don't know how many args I have to pop from the stack.
>And I can't get the number from a local variable, because I don't know the
>frame pointer anymore (I modified SP pushing the args before I called the
>function).

So save space for n + 1 parameters on the stack; the last won't be used
by your called function, and you could store the number of parameters in
it.  When you return, pop the last item off the stack, and use that to
recalculate the frame pointer.

	- Dan C.



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

* Re: [9fans] calling function with (variable number of) parameters
  2001-06-21 12:39   ` Douglas A. Gwyn
@ 2001-06-21 13:48     ` Latchesar Ionkov
  0 siblings, 0 replies; 12+ messages in thread
From: Latchesar Ionkov @ 2001-06-21 13:48 UTC (permalink / raw)
  To: 9fans

On Thu, Jun 21, 2001 at 12:39:50PM +0000, Douglas A. Gwyn said:
> Latchesar Ionkov wrote:
> > function returns, I don't know how many args I have to pop from the stack.
> > And I can't get the number from a local variable, because I don't know the
> > frame pointer anymore
>
> So use a global variable.  It's already a crock anyway..

the function has to be reentrant.

thanks for all the suggestions. obviously i'll reserve constant space in the
stack for the arguments.

	Lucho


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

* Re: [9fans] calling function with (variable number of) parameters
  2001-06-20 19:54 ` Latchesar Ionkov
@ 2001-06-21 12:39   ` Douglas A. Gwyn
  2001-06-21 13:48     ` Latchesar Ionkov
  2001-06-21 14:13   ` Dan Cross
  1 sibling, 1 reply; 12+ messages in thread
From: Douglas A. Gwyn @ 2001-06-21 12:39 UTC (permalink / raw)
  To: 9fans

Latchesar Ionkov wrote:
> function returns, I don't know how many args I have to pop from the stack.
> And I can't get the number from a local variable, because I don't know the
> frame pointer anymore

So use a global variable.  It's already a crock anyway..


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

* Re: [9fans] calling function with (variable number of) parameters
  2001-06-20 19:51 rog
@ 2001-06-20 19:54 ` Latchesar Ionkov
  2001-06-21 12:39   ` Douglas A. Gwyn
  2001-06-21 14:13   ` Dan Cross
  0 siblings, 2 replies; 12+ messages in thread
From: Latchesar Ionkov @ 2001-06-20 19:54 UTC (permalink / raw)
  To: 9fans

On Wed, Jun 20, 2001 at 08:51:03PM +0100, rog@vitanuova.com said:
> > The interface is called JNI and I can't change it.
> 
> presumably you could write a bit of assembler to get around the
> problem. you'd have to do it for each platform, but it wouldn't be hard
> and as far as i can see there wouldn't be a problem doing it: just push
> all the args on the stack (last first) & call the fn.

I've already tried to write it in assembler. The problem is that after the
function returns, I don't know how many args I have to pop from the stack.
And I can't get the number from a local variable, because I don't know the
frame pointer anymore (I modified SP pushing the args before I called the
function).

Thanks,
	Lucho


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

* Re: [9fans] calling function with (variable number of) parameters
@ 2001-06-20 19:51 rog
  2001-06-20 19:54 ` Latchesar Ionkov
  0 siblings, 1 reply; 12+ messages in thread
From: rog @ 2001-06-20 19:51 UTC (permalink / raw)
  To: 9fans

> The interface is called JNI and I can't change it.

presumably you could write a bit of assembler to get around the
problem. you'd have to do it for each platform, but it wouldn't be hard
and as far as i can see there wouldn't be a problem doing it: just push
all the args on the stack (last first) & call the fn.

  cheers,
    rog.



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

* Re: [9fans] calling function with (variable number of) parameters
  2001-06-20 16:30 ` Douglas A. Gwyn
@ 2001-06-20 18:23   ` Latchesar Ionkov
  0 siblings, 0 replies; 12+ messages in thread
From: Latchesar Ionkov @ 2001-06-20 18:23 UTC (permalink / raw)
  To: 9fans

On Wed, Jun 20, 2001 at 04:30:21PM +0000, Douglas A. Gwyn said:
> Latchesar Ionkov wrote:
> > I am trying to write a function
> >         int callf(void *f, int nargs, int *args)
> > f is a pointer to a function of a type
> >         int f(int, int, int, ..., int)
> 
> No, it isn't; at least, not as you have declared callf.
> 
> Also, that is not a proper variadic declaration for the type of f.
> I think what you mean is that the actual function definition uses
> a *fixed* number of arguments, different for different values of
> f, and you're hoping that the passed nargs happens to be
> consistent with the function that the passed f points to.

You understood what I meant, which was the purpose of the two pseudo
declarations.

> > callf should copy nargs number of ints from args to the stack and call f,
> > and then return f's return value.
> 
> > Is this possible with Plan9 C calling convention? ...
> > Do I miss something? Any ideas how to implement it?
> 
> It's an abuse of C under any linkage convention.

I know, but that knowledge doesn't help me.

> > Unfortunately I don't have any control on the requirements or the way f gets
> > its parameters. If there is no way to do it for any number of args, I'll
> > limit nargs (and hope nobody will write function with more than 15
> > arguments) and will modify SP with a constant value.
> 
> That will probably be your best bet.
> Other solutions would require changes to the interface.

The interface is called JNI and I can't change it.

	Lucho


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

* Re: [9fans] calling function with (variable number of) parameters
  2001-06-20 14:11 Latchesar Ionkov
@ 2001-06-20 16:30 ` Douglas A. Gwyn
  2001-06-20 18:23   ` Latchesar Ionkov
  0 siblings, 1 reply; 12+ messages in thread
From: Douglas A. Gwyn @ 2001-06-20 16:30 UTC (permalink / raw)
  To: 9fans

Latchesar Ionkov wrote:
> I am trying to write a function
>         int callf(void *f, int nargs, int *args)
> f is a pointer to a function of a type
>         int f(int, int, int, ..., int)

No, it isn't; at least, not as you have declared callf.

Also, that is not a proper variadic declaration for the type of f.
I think what you mean is that the actual function definition uses
a *fixed* number of arguments, different for different values of
f, and you're hoping that the passed nargs happens to be
consistent with the function that the passed f points to.

> callf should copy nargs number of ints from args to the stack and call f,
> and then return f's return value.

> Is this possible with Plan9 C calling convention? ...
> Do I miss something? Any ideas how to implement it?

It's an abuse of C under any linkage convention.

> Unfortunately I don't have any control on the requirements or the way f gets
> its parameters. If there is no way to do it for any number of args, I'll
> limit nargs (and hope nobody will write function with more than 15
> arguments) and will modify SP with a constant value.

That will probably be your best bet.
Other solutions would require changes to the interface.


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

* [9fans] calling function with (variable number of) parameters
@ 2001-06-20 14:11 Latchesar Ionkov
  2001-06-20 16:30 ` Douglas A. Gwyn
  0 siblings, 1 reply; 12+ messages in thread
From: Latchesar Ionkov @ 2001-06-20 14:11 UTC (permalink / raw)
  To: 9fans

Hi,

I am trying to write a function

	int callf(void *f, int nargs, int *args)
	
f is a pointer to a function of a type

	int f(int, int, int, ..., int)
	
callf should copy nargs number of ints from args to the stack and call f,
and then return f's return value.

The requirement is to not restrict nargs -- theoretically f can have 1000+
arguments.

Is this possible with Plan9 C calling convention? The caller function saves
the registers that it needs (obviously in the stack), then SP is modified
with a non-constant value, f is called, and after the execution returns in
callf, there is no any way go guess, what that value was.

Do I miss something? Any ideas how to implement it?

Unfortunately I don't have any control on the requirements or the way f gets
its parameters. If there is no way to do it for any number of args, I'll
limit nargs (and hope nobody will write function with more than 15
arguments) and will modify SP with a constant value.

Thanks,
	Lucho


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

end of thread, other threads:[~2001-06-29 22:11 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-06-21 14:33 [9fans] calling function with (variable number of) parameters rog
2001-06-21 15:16 ` Dan Cross
2001-06-21 15:17 ` Latchesar Ionkov
2001-06-29 22:11   ` Boyd Roberts
  -- strict thread matches above, loose matches on Subject: below --
2001-06-20 19:51 rog
2001-06-20 19:54 ` Latchesar Ionkov
2001-06-21 12:39   ` Douglas A. Gwyn
2001-06-21 13:48     ` Latchesar Ionkov
2001-06-21 14:13   ` Dan Cross
2001-06-20 14:11 Latchesar Ionkov
2001-06-20 16:30 ` Douglas A. Gwyn
2001-06-20 18:23   ` Latchesar Ionkov

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 NNTP newsgroup(s).