Bug 16308 - Error passing constant as argument in m68k and address mode problem
Summary: Error passing constant as argument in m68k and address mode problem
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: target (show other bugs)
Version: 3.4.0
: P1 critical
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2004-07-01 02:46 UTC by Vitor Hugo Almeida Marques
Modified: 2005-07-23 22:49 UTC (History)
1 user (show)

See Also:
Host:
Target: m68k-elf
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Vitor Hugo Almeida Marques 2004-07-01 02:46:15 UTC
It is important to enphasize that this bug was also verified in gcc 3.3.3.

When we compile a C code to the m68k architecture and in the C code we use a
constant as argument of a function, that's why the critical severity. The code
generated to the m68k is wrong.

The bug: this architecture, because it has few registers, uses the stack to
receive parameters. If a C code passes the value 5 to a function for example,
this value has to be store in the stack,after this has to be decremented by 4
(the stack goes up downward).
The code with this aim is normally generated with PEA(push effective address)
m68k coldfire family instrunction, and this instruction do not have a address
mode that loads directly the value to the stack, only what the value points  to
in memory. Therefore the code is generated this way, what is wrong.

In this example the codes generated would be pea 5.w 

The address mode told is (XXXX).W or (XXXX).L (described in m68k Coldfire family
manual). what problably occurs is a misunderstanding by the gcc about what this
address mode means. This cam be realized also with the use of JMP instruction
that has to jump downright to the address indicated by value x , but it jump to
address that value x points to im memory. That's what the parenthesis means, 
the operand is where XXXX points to.

A example in a fibonacci function:

C code:
int fibo(int);

int main(){
  int x;  
  x=fibo(10);    <---------- the constant value being passed
return 0;
}

int fibo(int n){
  if ((n==1)||(n==0)) return 1;
  else
    return (fibo(n-1)+fibo(n-2));
}


Assembly generated code:
#NO_APP
	.file	"fibonacci.c"
	.text
	.align	2
	.globl	main
	.type	main, @function
main:
	link.w %a6,#-4
	pea 10.w               <-------- It should put the value 10 in the stack
	jbsr fibo                        but puts what's in address 10
	addq.l #4,%sp
	move.l %d0,-4(%a6)
	clr.l %d0
	unlk %a6
	rts
	.size	main, .-main
	.align	2
	.globl	fibo
	.type	fibo, @function
fibo:
	link.w %a6,#-4
	move.l %d2,-(%sp)
	moveq #1,%d0
	cmp.l 8(%a6),%d0
	jbeq .L4
	tst.l 8(%a6)
	jbne .L3
.L4:
	moveq #1,%d0
	move.l %d0,-4(%a6)
	jbra .L2
	.align	2
.L3:
	move.l 8(%a6),%d0
	subq.l #1,%d0
	move.l %d0,-(%sp)
	jbsr fibo
	addq.l #4,%sp
	move.l %d0,%d2
	move.l 8(%a6),%d0
	subq.l #2,%d0
	move.l %d0,-(%sp)
	jbsr fibo
	addq.l #4,%sp
	add.l %d0,%d2
	move.l %d2,-4(%a6)
.L2:
	move.l -4(%a6),%d0
	move.l -8(%a6),%d2
	unlk %a6
	rts
	.size	fibo, .-fibo
	.ident	"GCC: (GNU) 3.4.0"

compilation line: 
m68k-elf-gcc -S fibonacci.c -o fibonaccireport.s

Compilation finished at Wed Jun 30 23:36:14




The gcc was compiled in Fedora linux with the folowing:

--target=m68k-elf --with-gnu-as --with-gnu-ld --with-newlib
--enable-languages=c++,c. The binutils was installed with the same target.
Comment 1 Andreas Schwab 2004-07-01 12:48:54 UTC
Reread the description of the pea (and lea) instruction, there is nothing wrong 
here.  "pea 10.w" is exactly equivalent to "move.l #10,-(%sp)", except that the 
former is smaller and faster.