Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!ut-sally!utah-cs!donn From: donn@utah-cs.UUCP (Donn Seeley) Newsgroups: comp.lang.c Subject: Re: Long Branches Message-ID: <4184@utah-cs.UUCP> Date: Tue, 20-Jan-87 03:32:56 EST Article-I.D.: utah-cs.4184 Posted: Tue Jan 20 03:32:56 1987 Date-Received: Tue, 20-Jan-87 21:39:02 EST References: <3950006@nucsrl.UUCP> <6552@alice.uUCp> <531@aw.sei.cmu.edu.sei.cmu.edu> <53900001@umn-cs.UUCP> Organization: University of Utah CS Dept Lines: 38 Summary: you can get around PCC/VAX limitations on switch statements 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@cs.utah.edu 40 46' 6"N 111 50' 34"W (801) 581-5668 utah-cs!donn