This is the mail archive of the gcc-bugs@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]
Other format: [Raw text]

[Bug c/16000] New: for h8 targets variadic functions are not working properly


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


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