chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Chicken-users] How does Chicken know the # of arguments to a functi


From: John Lenz
Subject: Re: [Chicken-users] How does Chicken know the # of arguments to a function
Date: Mon, 20 Dec 2004 08:34:43 +0000

On 12/20/04 00:25:17, felix winkelmann wrote:
On Sun, 19 Dec 2004 13:30:02 +0000, Joel Reymont <address@hidden> wrote:
> > felix winkelmann wrote:
>
> >Every primitive Chicken function takes one argument (the first one,
> >actually) that holds the number of arguments passed.
>
> But how does Chicken know how many arguments _to expect_?
>

It's compiled into the code:

(define (foo x y)
  (print x y) )

->

/* foo in k18 in k15 in k12 */
static void f_22(C_word c,C_word t0,C_word t1,C_word t2,C_word t3){
C_word tmp;
C_word t4;
C_word *a;
if(c!=4) C_bad_argc(c,4);           /*   <-- check (includes 2 implicit
args) */

Yeah, and because all arches push the parameters onto the stack in reverse order, so even if f_22 is called with a different number of parameters the first parameter c will still be valid. I guess on some arches like sparc the first few parameters are passed in registers, but then c is still valid too. And chicken can get away with it because if you call a function with more arguments than the function expects, normally that would leak stack space (unless the caller freed the args, guess depends on calling convention). But that isn't a problem for chicken because functions never return and the stack is collected with a longjmp.

Is that right?

Secondly, SWIG is not exporting this check right now. I should add them. If you call with more args the function just works because the extra arguments are sitting on the stack but the function never uses them. The only problem is when you call with less, then t2 or t3 could contain garbage. But SWIG does check types, so when I tried it with SWIG it gave me a type error. Now of course I could have gotten unlucky and had the random junk actually be a chicken data type AND have it be the data type expecting.

John






reply via email to

[Prev in Thread] Current Thread [Next in Thread]