Re: [C--] Segmentation fault (cannot take the address of a formal parameter)

Norman Ramsey <[email protected]> Mon, 30 Mar 2009 16:50:08 -0400
Newsgroups gmane.comp.lang.c--
Message-ID <[email protected]>
 > The formal parameters of a C-- procedure are always passed in 
 > registers.  This suggests that there is no corresponding C-- 
 > representation for the following C code:
 > 
 > int foo (int argc)
 > {
 >     int * ptr = & argc;
 >     printf ("%p\n", ptr);
 > }
 > 
 > I believe it's a known issue to C-- folks, since the manual mentions no 
 > support for "varargs".  But it's still a little surprise.

Actually this is incorrect.   The C compiler copies argc from its
register onto the stack.  There's a nice example in the book by Fraser
and Hanson.

There appears to be a bug in our back end such that if a local
variable's address is taken, it is forced to the stack, but not a
parameter.  I don't understand why not, because I see the code in the
back end.  Right now I can't build a working lcc, but the code should
look something like this:

  export @i_foo as "_foo";
  section "text" {
  foreign "C" @i_foo(bits32 @i_argc, ) {
    stackdata {
      align 4;
      @i_@0i_xxx: bits32;
    }
    bits32 @i_@1i_ptr;
    bits32[@i_@0i_xxx] = @i_argc;
    @i_@1i_ptr = @i_@0i_xxx;
    foreign "C" (bits32[@i_printf])(bits32[@i_2], @i_@1i_ptr);
    l@i_1:
    foreign "C" return();
  }
  import bits32 "_printf" as @i_printf;
  }
  section "data" {
  @i_2:
  bits8[] { 37::bits8, 112::bits8, 10::bits8, 0::bits8, };
  }


Norman
_______________________________________________
Cminusminus mailing list
[email protected]
https://www.eecs.harvard.edu/mailman/listinfo/cminusminus