Long Branches

Donn Seeley donn at utah-cs.UUCP
Tue Jan 20 18:32:56 AEST 1987


Renu Raman asks what one must do in order to compile a huge switch
statement on a VAX using the 4.3 BSD PCC.  The real answer, as has been
suggested by Robert Firth, is to use structured coding techniques to
avoid creating monstrous Fortran-style functions.  Sometimes one must
contend with mechanically generated C code, however, and in such cases
it can be useful to know about hacks to squeeze such awful code through
the compiler.

As Robert Firth says, the VAX CASEx instructions are limited to 16-bit
branch offsets; it's this restriction which produces the assembler
warning 'Case will branch too far'.  (Other VAX instructions which have
unexpectedly limited displacement sizes include ACBx, AOBxxx and
SOBxxx...)  It is possible to force the VAX 4.3 BSD PCC to produce
actual tests and jumps instead of a CASEL, however.  Unfortunately this
is not conditioned on a compiler flag, so the technique qualifies as a
hack.

The compiler will use a 'heap' switch if there are more than 8 case
labels and the difference between the greatest and the least case
labels is greater than three times the number of labels (this controls
the density of the CASEL branch table).  A typical switch uses
low-numbered case labels; if you throw in a 'case 0x7fffffff:' with the
default label, this should force the compiler to use a 'heap' switch
instead of a CASEL switch.  A 'heap' switch is a jumble of tests and
branches; if these are assembled with the -J flag, they will be able to
span a distance of greater than 32 Kb.

Robert Herndon suggested that the peephole optimizer can 'fix' huge
switch statements.  The peephole optimizer won't replace CASEL
switches, but it will sometimes rearrange code so that a switch will
squeeze inside the range.  I don't know if the peephole optimizer will
handle most 'typical' huge switches, but I was able to cook up an
example just now which was beyond its capacity, so it's not a panacea.

People can produce Fortran regardless of the actual coding language,

Donn Seeley    University of Utah CS Dept    donn at cs.utah.edu
40 46' 6"N 111 50' 34"W    (801) 581-5668    utah-cs!donn



More information about the Comp.lang.c mailing list