public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/16000] New:  for h8 targets variadic functions are not working properly
@ 2004-06-15 14:05 manishas at kpitcummins dot com
  2004-06-15 14:08 ` [Bug target/16000] " pinskia at gcc dot gnu dot org
                   ` (5 more replies)
  0 siblings, 6 replies; 8+ messages in thread
From: manishas at kpitcummins dot com @ 2004-06-15 14:05 UTC (permalink / raw)
  To: gcc-bugs

Consider the  attached test case for H8 target in which variadic function is 
declared to take variable number of  arguments but in the definition fixed 
number of arguments are used.  This is allowed in gcc as per document in  
http://www.gnu.org/software/libc/manual/html_node/Why-Variadic.html#Why%
20Variadic

Following code does not pass arguments properly to the function. The variadic 
function gives 
wrong results because  parameters are passed on stack and the function is 
expects parameters in registers. Which can be observed from the following 
assembly code
	
mov.w	#3,r2
mov.l er2, @-er7 // push arg3 on stack
sub.l	er2,er2
add.b	#2,r2l
mov.l er2, @-er7 // push arg2 on stack
mov.w	#1,r0 // move arg1 in er0
jsr	@_variadic_test// call variadic_test function.

The register er7 is the stack pointer. So the arguments 2 and 3 are pushed onto 
stack and argument1 is in register r0.And this is the assembler code in the 
function variadic_test immediately after the jsr instruction above is executed:

mov.l er6, @-er7 // save frame pointer
mov.l er7,er6
.
.
mov.w r0,@(-2,er6) // get arg 1 from r0
mov.l er1,@(-8,er6) // get arg 2 from er1
mov.l er2,@(-12,er6) // get arg 3 from er2


The function sets up the stack and then copies the function arguments from the 
registers r0, er1 and er2 onto the stack. But because of the different function 
declaration the registers r0, er1 and er2 do not contain the expected values.

command line for assembly code:
h8300-elf-gcc -S -ms variadic.c  main.c

command line for output file: 
h8300-elf-gcc  -o  main.out  variadic.c main.c

------------variadic.c------------------------------------

int variadic_test(int arg1, unsigned long arg2, unsigned long arg3)
{

    int           ret;


    ret = arg1 + arg2 + arg3;

    return ret;
}
----------------------------------------------------------------
------------------ main.c ----------------------------------------------
int variadic_test (int, unsigned long, ...);

int main (void)
{
    int result_varfunc;
    int result_local;

    result_local   =  1 + 2 + 3;
    result_varfunc =  variadic_test(1, 2, 3);

    if (result_local != result_varfunc)
    {
        //printf("error");

        return 0;
    }
    else
    {

        return 1;
    }
}

-- 
           Summary:  for h8 targets variadic functions are not working
                    properly
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: manishas at kpitcummins dot com
                CC: gcc-bugs at gcc dot gnu dot org
  GCC host triplet: windows 2000
GCC target triplet: h8


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=16000


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

end of thread, other threads:[~2004-08-16  7:17 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-06-15 14:05 [Bug c/16000] New: for h8 targets variadic functions are not working properly manishas at kpitcummins dot com
2004-06-15 14:08 ` [Bug target/16000] " pinskia at gcc dot gnu dot org
2004-06-15 14:12 ` manishas at kpitcummins dot com
2004-06-15 14:27   ` Graham Stott
2004-06-15 14:27 ` graham dot stott at btinternet dot com
2004-06-15 15:26 ` bonzini at gnu dot org
2004-06-15 15:27 ` bonzini at gnu dot org
2004-08-16  7:17 ` [Bug other/16000] " kazu at cs dot umass dot edu

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).