1 /* Copyright (c) 1980 Regents of the University of California */ 2 3 static char sccsid[] = "@(#)pccaseop.c 1.8.1.2 01/17/83"; 4 5 #include "whoami.h" 6 #ifdef PC 7 /* 8 * and the rest of the file 9 */ 10 #include "0.h" 11 #include "tree.h" 12 #include "objfmt.h" 13 #include "pcops.h" 14 #include "pc.h" 15 16 /* 17 * structure for a case: 18 * its constant label, line number (for errors), and location label. 19 */ 20 struct ct { 21 long cconst; 22 int cline; 23 int clabel; 24 }; 25 26 /* 27 * the P2FORCE operator puts its operand into a register. 28 * these to keep from thinking of it as r0 all over. 29 */ 30 #ifdef vax 31 # define FORCENAME "r0" 32 #endif vax 33 #ifdef mc68000 34 # define FORCENAME "d0" 35 #endif mc68000 36 37 /* 38 * given a tree for a case statement, generate code for it. 39 * this computes the expression into a register, 40 * puts down the code for each of the cases, 41 * and then decides how to do the case switching. 42 * tcase [0] T_CASE 43 * [1] lineof "case" 44 * [2] expression 45 * [3] list of cased statements: 46 * cstat [0] T_CSTAT 47 * [1] lineof ":" 48 * [2] list of constant labels 49 * [3] statement 50 */ 51 pccaseop( tcase ) 52 int *tcase; 53 { 54 struct nl *exprtype; 55 struct nl *exprnlp; 56 struct nl *rangetype; 57 long low; 58 long high; 59 long exprctype; 60 long swlabel; 61 long endlabel; 62 long label; 63 long count; 64 long *cstatlp; 65 long *cstatp; 66 long *casep; 67 struct ct *ctab; 68 struct ct *ctp; 69 long i; 70 long nr; 71 long goc; 72 int casecmp(); 73 bool dupcases; 74 75 goc = gocnt; 76 /* 77 * find out the type of the case expression 78 * even if the expression has errors (exprtype == NIL), continue. 79 */ 80 line = tcase[1]; 81 codeoff(); 82 exprtype = rvalue( (int *) tcase[2] , NIL , RREQ ); 83 codeon(); 84 if ( exprtype != NIL ) { 85 if ( isnta( exprtype , "bcsi" ) ) { 86 error("Case selectors cannot be %ss" , nameof( exprtype ) ); 87 exprtype = NIL; 88 } else { 89 if ( exprtype -> class != RANGE ) { 90 rangetype = exprtype -> type; 91 } else { 92 rangetype = exprtype; 93 } 94 if ( rangetype == NIL ) { 95 exprtype = NIL; 96 } else { 97 low = rangetype -> range[0]; 98 high = rangetype -> range[1]; 99 } 100 } 101 } 102 if ( exprtype != NIL ) { 103 /* 104 * compute and save the case expression. 105 * also, put expression into a register 106 * save its c-type and jump to the code to do the switch. 107 */ 108 exprctype = p2type( exprtype ); 109 exprnlp = tmpalloc( sizeof (long) , nl + T4INT , NOREG ); 110 putRV( 0 , cbn , exprnlp -> value[ NL_OFFS ] , 111 exprnlp -> extra_flags , P2INT ); 112 (void) rvalue( (int *) tcase[2] , NIL , RREQ ); 113 putop( P2ASSIGN , P2INT ); 114 putop( P2FORCE , P2INT ); 115 putdot( filename , line ); 116 swlabel = getlab(); 117 putjbr( swlabel ); 118 } 119 /* 120 * count the number of cases 121 * and allocate table for cases, lines, and labels 122 * default case goes in ctab[0]. 123 */ 124 count = 1; 125 for ( cstatlp = tcase[3] ; cstatlp != NIL ; cstatlp = cstatlp[2] ) { 126 cstatp = cstatlp[1]; 127 if ( cstatp == NIL ) { 128 continue; 129 } 130 for ( casep = cstatp[2] ; casep != NIL ; casep = casep[2] ) { 131 count++; 132 } 133 } 134 /* 135 */ 136 ctab = (struct ct *) malloc( count * sizeof( struct ct ) ); 137 if ( ctab == (struct ct *) 0 ) { 138 error("Ran out of memory (case)"); 139 pexit( DIED ); 140 } 141 /* 142 * pick up default label and label for after case statement. 143 */ 144 ctab[0].clabel = getlab(); 145 endlabel = getlab(); 146 /* 147 * generate code for each case 148 * filling in ctab for each. 149 * nr is for error if no case falls out bottom. 150 */ 151 nr = 1; 152 count = 0; 153 for ( cstatlp = tcase[3] ; cstatlp != NIL ; cstatlp = cstatlp[2] ) { 154 cstatp = cstatlp[1]; 155 if ( cstatp == NIL ) { 156 continue; 157 } 158 line = cstatp[1]; 159 label = getlab(); 160 for ( casep = cstatp[2] ; casep != NIL ; casep = casep[2] ) { 161 gconst( casep[1] ); 162 if( exprtype == NIL || con.ctype == NIL ) { 163 continue; 164 } 165 if ( incompat( con.ctype , exprtype , NIL ) ) { 166 cerror("Case label type clashed with case selector expression type"); 167 continue; 168 } 169 if ( con.crval < low || con.crval > high ) { 170 error("Case label out of range"); 171 continue; 172 } 173 count++; 174 ctab[ count ].cconst = con.crval; 175 ctab[ count ].cline = line; 176 ctab[ count ].clabel = label; 177 } 178 /* 179 * put out the statement 180 */ 181 putlab( label ); 182 putcnt(); 183 level++; 184 statement( cstatp[3] ); 185 nr = (nr && noreach); 186 noreach = 0; 187 level--; 188 if (gotos[cbn]) { 189 ungoto(); 190 } 191 putjbr( endlabel ); 192 } 193 noreach = nr; 194 /* 195 * default action is to call error 196 */ 197 putlab( ctab[0].clabel ); 198 putleaf( P2ICON , 0 , 0 , ADDTYPE( P2FTN | P2INT , P2PTR ) , "_CASERNG" ); 199 putRV( 0 , cbn , exprnlp -> value[ NL_OFFS ] , 200 exprnlp -> extra_flags , P2INT ); 201 putop( P2CALL , P2INT ); 202 putdot( filename , line ); 203 /* 204 * sort the cases 205 */ 206 qsort( &ctab[1] , count , sizeof (struct ct) , casecmp ); 207 /* 208 * check for duplicates 209 */ 210 dupcases = FALSE; 211 for ( ctp = &ctab[1] ; ctp < &ctab[ count ] ; ctp++ ) { 212 if ( ctp[0].cconst == ctp[1].cconst ) { 213 error("Multiply defined label in case, lines %d and %d" , 214 ctp[0].cline , ctp[1].cline ); 215 dupcases = TRUE; 216 } 217 } 218 if ( dupcases ) { 219 return; 220 } 221 /* 222 * choose a switch algorithm and implement it: 223 * direct switch >= 1/3 full and >= 4 cases. 224 * binary switch not direct switch and > 8 cases. 225 * ifthenelse not direct or binary switch. 226 */ 227 putlab( swlabel ); 228 if ( ctab[ count ].cconst - ctab[1].cconst < 3 * count && count >= 4 ) { 229 directsw( ctab , count ); 230 } else if ( count > 8 ) { 231 binarysw( ctab , count ); 232 } else { 233 itesw( ctab , count ); 234 } 235 putlab( endlabel ); 236 if ( goc != gocnt ) { 237 putcnt(); 238 } 239 } 240 241 /* 242 * direct switch 243 */ 244 directsw( ctab , count ) 245 struct ct *ctab; 246 int count; 247 { 248 int fromlabel = getlab(); 249 long i; 250 long j; 251 252 # ifdef vax 253 putprintf(" casel %s,$%d,$%d" , 0 , FORCENAME , 254 ctab[1].cconst , ctab[ count ].cconst - ctab[1].cconst ); 255 # endif vax 256 # ifdef mc68000 257 /* 258 * subl to make d0 a 0-origin byte offset. 259 * cmpl check against upper limit. 260 * bhi error if out of bounds. 261 * addw to make d0 a 0-origin word offset. 262 * movw pick up a jump-table entry 263 * jmp and indirect through it. 264 */ 265 putprintf(" subl #%d,%s", 0, ctab[1].cconst, FORCENAME); 266 putprintf(" cmpl #%d,%s", 0, 267 ctab[count].cconst - ctab[1].cconst, FORCENAME); 268 putprintf(" bhi %s%d", 0, LABELPREFIX, ctab[0].clabel); 269 putprintf(" addw %s,%s", 0, FORCENAME, FORCENAME); 270 putprintf(" movw pc@(6,%s:w),%s", 0, FORCENAME, FORCENAME); 271 putprintf(" jmp pc@(2,%s:w)", 0, FORCENAME); 272 # endif mc68000 273 putlab( fromlabel ); 274 i = 1; 275 j = ctab[1].cconst; 276 while ( i <= count ) { 277 if ( j == ctab[ i ].cconst ) { 278 putprintf( " .word " , 1 ); 279 putprintf( PREFIXFORMAT , 1 , LABELPREFIX , ctab[ i ].clabel ); 280 putprintf( "-" , 1 ); 281 putprintf( PREFIXFORMAT , 0 , LABELPREFIX , fromlabel ); 282 i++; 283 } else { 284 putprintf( " .word " , 1 ); 285 putprintf( PREFIXFORMAT , 1 , LABELPREFIX , ctab[ 0 ].clabel ); 286 putprintf( "-" , 1 ); 287 putprintf( PREFIXFORMAT , 0 , LABELPREFIX , fromlabel ); 288 } 289 j++; 290 } 291 # ifdef vax 292 /* 293 * execution continues here if value not in range of case. 294 */ 295 putjbr( ctab[0].clabel ); 296 # endif vax 297 } 298 299 /* 300 * binary switch 301 * special case out default label and start recursion. 302 */ 303 binarysw( ctab , count ) 304 struct ct *ctab; 305 int count; 306 { 307 308 bsrecur( ctab[0].clabel , &ctab[0] , count ); 309 } 310 311 /* 312 * recursive log( count ) search. 313 */ 314 bsrecur( deflabel , ctab , count ) 315 int deflabel; 316 struct ct *ctab; 317 int count; 318 { 319 320 if ( count <= 0 ) { 321 putjbr(deflabel); 322 return; 323 } else if ( count == 1 ) { 324 # ifdef vax 325 putprintf(" cmpl %s,$%d", 0, FORCENAME, ctab[1].cconst); 326 putprintf(" jeql %s%d", 0, LABELPREFIX, ctab[1].clabel); 327 putjbr(deflabel); 328 # endif vax 329 # ifdef mc68000 330 putprintf(" cmpl #%d,%s", 0, ctab[1].cconst, FORCENAME); 331 putprintf(" jeq L%d", 0, LABELPREFIX, ctab[1].clabel); 332 putjbr(deflabel); 333 # endif mc68000 334 return; 335 } else { 336 int half = ( count + 1 ) / 2; 337 int gtrlabel = getlab(); 338 339 # ifdef vax 340 putprintf(" cmpl %s,$%d", 0, FORCENAME, ctab[half].cconst); 341 putprintf(" jgtr %s%d", 0, LABELPREFIX, gtrlabel); 342 putprintf(" jeql %s%d", 0, LABELPREFIX, ctab[half].clabel); 343 # endif vax 344 # ifdef mc68000 345 putprintf(" cmpl #%d,%s", 0, ctab[half].cconst, FORCENAME); 346 putprintf(" jgt %s%d", 0, LABELPREFIX, gtrlabel); 347 putprintf(" jeq %s%d", 0, LABELPREFIX, ctab[half].clabel); 348 # endif mc68000 349 bsrecur( deflabel , &ctab[0] , half - 1 ); 350 putlab(gtrlabel); 351 bsrecur( deflabel , &ctab[ half ] , count - half ); 352 return; 353 } 354 } 355 356 itesw( ctab , count ) 357 struct ct *ctab; 358 int count; 359 { 360 int i; 361 362 for ( i = 1 ; i <= count ; i++ ) { 363 # ifdef vax 364 putprintf(" cmpl %s,$%d", 0, FORCENAME, ctab[i].cconst); 365 putprintf(" jeql %s%d", 0, LABELPREFIX, ctab[i].clabel); 366 # endif vax 367 # ifdef mc68000 368 putprintf(" cmpl #%d,%s", 0, ctab[i].cconst, FORCENAME); 369 putprintf(" jeq %s%d", 0, LABELPREFIX, ctab[i].clabel); 370 # endif mc68000 371 } 372 putjbr(ctab[0].clabel); 373 return; 374 } 375 int 376 casecmp( this , that ) 377 struct ct *this; 378 struct ct *that; 379 { 380 if ( this -> cconst < that -> cconst ) { 381 return -1; 382 } else if ( this -> cconst > that -> cconst ) { 383 return 1; 384 } else { 385 return 0; 386 } 387 } 388 #endif PC 389