[gnu.gcc.bug] gcc1.35 bites man with large switch statements.

glenne@HPLSLA.HP.COM (Glenn Engel) (06/20/89)

I am using gcc1.35 configured for hp9k320g.

Large (code content) switch statements can cause gcc to generate bad code.
A switch statement like this:
main()
{
int i;
switch (i)
{
  case 1: geeWhiz(); break;
  case 2: geeWhiz(); break;
  case 3: geeWhiz(); break;
  case 4: geeWhiz(); break;
}
}
will generate mc680x0 code like this:

#NO_APP
gcc_compiled.:
.text
	.even
.globl _main
_main:
	link a6,#-4
	movel a6@(-4),d0
	subql #1,d0
	moveq #3,d1
	cmpl d1,d0
	jhi L8
LI7:
	movew pc@(L7-LI7-2:b,d0:l:2),d0
	jmp pc@(2,d0:w)
L7:
	.word L3-L7
	.word L4-L7
	.word L5-L7
	.word L6-L7
L3:
	jbsr _geeWhiz
	jra L2
L4:
	jbsr _geeWhiz
	jra L2
L5:
	jbsr _geeWhiz
	jra L2
L6:
	jbsr _geeWhiz
	jra L2
L8:
L2:
L1:
	unlk a6
	rts

This code has several problems.  If more than 32767 bytes of code exist
in the body of the switch statement, the table offsets for the jump 
table will overflow a twos complement word.  Also, the jhi instruction
before the switch (and any break statements) can overflow the ability
of the 68000 to do pc relative branches and generate:
   /tmp/cca02007.s:20267:Long branch offset not supported.

When compiling for the 68000, an error will be generated for the branches
but not the .word <offset> values.  When compiling for the 68020, the
branches will be expanded to long offsets but the truncated table offsets
will be silently accepted.

In some cases a file will work fine when compiled with -O but fail
without -O due to the decrease in code size with -O.  The file glenne.c
which was previously ftp'ed for debugging is an example of this.

To summarize, the file glenne.c will have the following problems:

gcc -O -m68020     works fine. (can be bad with more code)
gcc -m68020        Will generate run-time error by jumping into ozone.
gcc -O -m68000     works fine. (can be bad with more code)
gcc -m68000        assembler errors about long branch offsets.

--
 |  Glenn R. Engel
 |  Hewlett-Packard 
 |  (206) 335-2066
 |  glenne%hplsla@hplabs.hp.com