Subject: Compiler bug in g++ 2.7.2.1
To: None <port-arm32@NetBSD.ORG>
From: Adam Gundy <adam@impala.demon.co.uk>
List: port-arm32
Date: 06/13/1997 21:23:47
I've found a nasty bug in g++ 2.7.2.1 (from RiscBSD 1.2 Release with
the patch applied).

Passing a 'bool' to a function as the fifth (or greater) argument
causes the stack pointer to stop being word aligned (result - SEGV).

The following code shows the problem:

------8<----------
#include <stdio.h>

#define TRUE 1
#define FALSE 0

// #define bool signed char

void
test_function( int x, int y, int z, int w, bool b )
{
   volatile int flarb;  // force the variable to be on the stack

   flarb = 1;
   fprintf( stderr, "%d\n", flarb );
}  

int
main( int argc, char **argv )
{
   test_function( 1, 2, 3, 4, TRUE );
   return 0;
}

-----------

this code generates the following assembler output with 'g++ -S':

@ Generated by gcc 2.7.2.1 for ARM/NetBSD
rfp	.req	r9
sl	.req	r10
fp	.req	r11
ip	.req	r12
sp	.req	r13
lr	.req	r14
pc	.req	r15
gcc2_compiled.:
___gnu_compiled_cplusplus:
.text
	.align	0
LC0:
	.ascii	"%d\012\000"
	.align	0
LC1:
	.word	LC0
	.align	0
LC2:
	.word	___sF+176
	.align	0
	.global	_test_function__Fiiiib
	.type	 _test_function__Fiiiib,@function
_test_function__Fiiiib:
	@ args = 4, pretend = 0, frame = 20
	@ frame_needed = 1, current_function_anonymous_args = 0
	mov	ip, sp
	stmfd	sp!, {fp, ip, lr, pc}
	sub	fp, ip, #4
	sub	sp, sp, #20
	str	r0, [fp, #-16]
	str	r1, [fp, #-20]
	str	r2, [fp, #-24]
	str	r3, [fp, #-28]
	mov	r3, #1
	str	r3, [fp, #-32]
	ldr	r2, [fp, #-32]
	ldr	r1, [pc, #LC1 - . - 8]
	ldr	r0, [pc, #LC2 - . - 8]
	bl	_fprintf
	b	L7
	b	L6
L7:
L6:
	ldmea	fp, {fp, sp, pc}
Lfe1:
	.size	 _test_function__Fiiiib,Lfe1-_test_function__Fiiiib
	.align	0
	.global	_main
	.type	 _main,@function
_main:
	@ args = 0, pretend = 0, frame = 8
	@ frame_needed = 1, current_function_anonymous_args = 0
	mov	ip, sp
	stmfd	sp!, {fp, ip, lr, pc}
	sub	fp, ip, #4
	sub	sp, sp, #8
	str	r0, [fp, #-16]
	str	r1, [fp, #-20]
	bl	___main
	mov	r3, #1
	mov	r2, r3
	strb	r2, [sp, #-1]!   <--- BUG! should be -4
	mov	r3, #4
	mov	r2, #3
	mov	r1, #2
	mov	r0, #1
	bl	_test_function__Fiiiib
	add	sp, sp, #4
	mov	r0, #0
	b	L8
	mov	r0, #0
	b	L8
L8:
	ldmea	fp, {fp, sp, pc}
Lfe2:
	.size	 _main,Lfe2-_main

----------

The compiler is attempting to byte align the argument on the stack,
rather than word align it (a bool passed as argument 1-4 works fine
because they're passed in registers).

My current hacky fix is the commented '#define bool signed char',
which produces correct assembly.

A fixed version of g++ would be greatly appreciated - i'm currently
suffering from a distinct lack of hard disk space, or I'd fix it
myself ;-)

Seeya,
 Adam.
-- 
As the year 2000 approaches, the carefully planned Millenium 'bug'
begins to manifest itself in the computing job market...
Real programmers don't comment their code. If it was hard to write, it
should be harder to modify. These are all my own opinions.